JavaScript Flashcards
get ASCII code of a letter
“abcd”.charCodeAt(0)
get letter from ASCII code
String.fromCharCode(97)
string to array
str.split(“”) or str.split(“,”) or str.split(/[|,]/g)
match a string in a sentence
let text = “The rain in SPAIN stays mainly in the plain”;
let matchArray = text.match(/ain/gi);
Check if a string includes “world”
text.includes(“world”);
to return a decimal after a specified precision (rounded off)
let x = 9.656;
x.toFixed(0);
x.toFixed(2);
x.toFixed(4);
Check if the value is an integer
Number.isInteger(x)
sorting an integer array
array.sort() // asending
array.sort().reverse() // descending
array.sort( (a, b) => b - a )
sorting a string array
array.sort() // asending
array.sort().reverse() // descending
array.sort( (a,b) => b.localeCompare(a) ) ) // descending
convert array into string ?
array.join(“”)
concatenating 2 arrays
const mergedArray = array1.concat(array2);
splice() to Remove Elements from Array
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.splice(0, 1); // [“Orange”, “Apple”, “Mango”];
take a slice of array
const fruits = [“Banana”, “Orange”, “Lemon”, “Apple”, “Mango”]; const citrus = fruits.slice(1, 3); // [“Orange”, “Lemon”]
Create an array from a string
let array = Array.from(“ABCDEFG”)
it has same effect as let array = “ABCDEFG”.split(“”)
Print all values of an array
for(let x of array) {
console.log(x)
}
// prints the values. “of” is for values