Concat “foo” and “bar” (all methods)
"foo" + "bar""foo".concat("bar")'${"foo"}${"bar"}'
Check if c in string cudacadca. Find position of c (first and last)
"cudacadca".includes("c")"cudacadca".indexOf("c")"cudacadca".lastIndexOf("c")
Check if str starts or ends with foo
str.startsWith("foo")str.endsWith("foo")
Convert str to upper or lower case.
str.toUpperCase()str.toLowerCase()
Remove spaces from beginning, end of a str (or both)
str.trimStart()str.trimEnd()str.trim()
Pad a string with 0 until it’s 10 chars wide (from left and right)
str.padStart(10, "0")str.padEnd(10, "0")
Extract substring"bar" from "foo bar baz" (using indexes)
"foo bar baz".slice(4, 7)"foo bar baz".substring(4, 7)
Extract last 3 chars from str
str.slice(-3)str.slice(-3, str.length)
Repalce all f with x in a str. Replace only first one.
str.replaceAll("f", "x")str.replace("f", "x")
Convert str into array of chars. "abc" => [ 'a', 'b', 'c' ]
str.split("")
Join [ 'a', 'b', 'c' ] with -
[ 'a', 'b', 'c' ].join("-")
Exercise
Hide all but last 4 chars of a string.
2034399002125581 => ************5581
const fullNumber = '2034399002125581'; const last4Digits = fullNumber.slice(-4); const maskedNumber = last4Digits.padStart(fullNumber.length, '*');
input.slice(-4).padStart(input.length, "*")
Check if second char of str is f (two ways).
What will happen if string length is 1 (there’s no second char).
str[1] == "f" // undefined if not foundstr.charAt(1) // “” if not found
Create string that is N times “X”
"X".repeat(n)
How to safely compare unicode strings and why.
str.normalize() === str2.normalize()
U+00E9)U+0065 U+0301)Construct string with new, print it’s value.
const foo = new String("foo");
console.log(foo.valueOf());
console.log(foo.toString()); // alternative
console.log(foo); // Displays: [String: 'foo'] !!!Create a string without parsing it’s escape characters (like \n, \ etc…)
String.raw`C:\Development\profile\aboutme.html`;