Vanilla JS Flashcards
How do you create Undefined?
let firstName;
console.log(firstName);
What does typeof operator do?
console. log(typeof ‘Iman Sayah’) // string
console. log(typeof 5) // number
console. log(typeof true) // boolean
console. log(typeof null) // object type
console. log(typeof undefined) // undefined
Primitive data types are immutable? Example?
let word = "JavaScript" word[0] = "K" // error
What are primitive data types in JavaScript?
Numbers - Integers, floats
Strings - Any data under single quote, double quote or backtick quote
Booleans - true or false value
Null - empty value or no value
Undefined - a declared variable without a value
What are non-primitive data type in JavaScript?
Objects
Functions
Arrays
Which data types are immutable?
a) primitive
b) non-primitive
primitive
How do you create a random number between 0 and 0.999999?
Math.random()
Math.round() Math.floor() Math.ceil() Math.min() Math.max()
round to nearest int round down to nearset int round up to nearest int min of a list of values: Math.min(1,2,3) max of a list of values: Math.max(1,2,3)
How do you generate a number between 0 and 10?
let randomNum = Math.random() // generates 0 to 0.999 let numBtnZeroAndTen = randomNum * 11 // this gives: min 0 and max 10.99 let randomNumRoundToFloor = Math.floor(numBtnZeroAndTen)
// Shortened let randNum = Math.floor(Math.random() * 10 + 1)
Which method is preferred for concatenating two strings?
A - Using “+”
B - Using ``
B - Back-ticks
console.log(The sum of ${a} and ${b} is ${a + b}
)
.length
length of string
.toUpperCase()
all capital letters
.toLowerCase()
all lower case letters
.substr(start, count)
start from starting index and return n characters.
DEPRECATED!!!
.substring(start, end)
.substring(start)
The substring() method returns the part of the string between the start and end indexes, or to the end of the string.
.split(by, count)
cuts out the string by substring and return n chunks
What is the output of
let myName = “Iman Sayah”
console.log(myName.split(‘a’));
[ ‘Im’, ‘n S’, ‘y’, ‘h’ ]
What is the output of
let myName = “Iman Sayah”
console.log(myName.split(‘a’, 2));
[ ‘Im’, ‘n S’ ]
What is the output of
let myName = “Iman Sayah”
console.log(myName.split(‘’));
[
‘I’, ‘m’, ‘a’, ‘n’, ‘ ‘, ‘S’, ‘a’, ‘y’, ‘a’, ‘h’
]
What is the output of
let myName = “Iman Sayah”
console.log(myName.split(‘ ‘));
[ ‘Iman’, ‘Sayah’ ]
What is the output of
let myName = “Iman Sayah”
console.log(myName.split(‘X’));
[ ‘Iman Sayah’ ]
.trim()
Removes the leading and trailing white space and line terminator characters from a string.
.includes(substring)
checks if substring argument exists in the string
.replace(substring, new)
replaces substring with new substring