JS Basic Tutorial
Method |
Description |
Example |
---|---|---|
Returns the length of the string |
let str = "whereisstuff"; str.length // output 12 |
|
Returns the character index of the string |
let str = "hello"; str[0] // output "h" str.charAt(0) // output "h" |
|
Returns the uppercase string |
let str = "Hello"; str.toUpperCase(); // output "HELLO" |
|
Returns the lowercase string |
let str = "WORLD"; str.toLowerCase(); // output "world" |
|
Returns the index of the first occurrence |
let str = "JavaScript is fun"; str.indexOf("is"); // 11 |
|
Checks if the substring exists |
let str = "JavaScript is fun"; str.includes("fun"); // true str.includes("bun"); // false |
|
Checks the start of string |
let str = "JavaScript is fun"; str.startsWith("Java"); // true str.startsWith("fun"); // false |
|
Checks the end of string |
let str = "JavaScript is fun"; str.endsWith("Java"); // false str.endsWith("fun"); // true |
|
slice string |
let str = "JavaScript is fun"; str.slice(0, 4); // "Java" |
|
Joins two or more strings |
let str = "JavaScript"; str.concat(" is fun"); // "JavaScript is fun" |
|
Removes whitespace |
let str = " JavaScript "; str.trim(); // "JavaScript" |
|
Replaces a substring |
let str = "JavaScript"; str.replace("Java", "Hello"); // "HelloScript" |
|
Splits a string into an array |
let str = "Java Script"; str.split(" "); // ["Java","Script"] |
|
Repeats a string |
let str = "Hi"; str.repeat(3); //HiHiHi |
|
Pads a string |
let str = "5"; str.padStart(3, "0") // "0005" str.padEnd(3, "0") // "5000" |
|
Convert string to a number |
let str = "5"; Number(str) // 5 |
|
Convert string to a number |
let str = "5"; Number(str) // 5 let str2 = "20px"; Number(str2) // 20 |
|
Convert string to a float number |
let str = "123.45"; parseFloat(str) // 123.45 |
Returns the length of the string
Returns the character index of the string
toUppercase()
→ Returns the uppercase string
toLowercase()
→ Returns the Lowercase string
Try it yourself
indexOf()
→ Returns the index of the first occurrenceincludes()
→ Checks if the substring existsstartsWith()
/ endsWith()
→ Checks the start/endTry it yourself
slice(start, end)
substring(start, end)
Try it yourself
concat()
→ Joins two or more stringstrim()
→ Removes whitespacereplace()
→ Replaces a substringsplit()
→ Splits a string into an arrayTry it yourself
repeat()
→ Repeats a stringpadStart()
/ padEnd()
→ Pads a stringTry it yourself
Number()
→ Convert string to a numberparseInt()
/ parseFloat()
Try it yourself