Variables, Types, and Collections Flashcards
8 data types
string, bigint, symbol, object, null, and undefined
let and const
block scoped
block = anything inside curly braces
when we don’t assign a value to a variable
undefined
except with const (you will get SyntaxError: missing initializer in const declaration)
check typeof
if (typeof role == ‘undefined’)
which three data types have wrapper functions?
Boolean, Number, String
which two datatypes represent absence of data?
null, undefined
typecasting methods
Number()
String()
Boolean()
toString() - used to covered a number to a string
parseInt() - parse a string argument to an integer or NaN
parseFloat() - method used to parse an argument to floating point
array construction
var brands = ["Fender", "VOX", "Line6"] var brands = new Array("Fender", "VOX", "Line6")
Number type in JS
represents any number between -(2^53 - 1) and (2^53 - 1)
most common string methods
toLowerCase()
toUpperCase()
concat(str [,…str]) - combines two or more strings and returns a new string
includes(searchString, [,position])
indexOf(searchValue [, fromIndex]) = returns the index of a value or -1 if not found
replace(searchFor, replaceWith)
substring(indexStart [, indexEnd]) - the substring() method returns a part of the string between the start and end indexes or the start index to the end of the string
search()
padStart() & padEnd()
trim()
common methods for Number object
isNaN() - static method can be used to determine whether a value is NaN
isInteger()
parseFloat(str) & parseInt(str [,radix])
toFixed(digits) - returns number that has the specified number of digits after the decimal point
toString([radix])
valueOf() - returns primitive value of specified number object
common date methods
now() = returns current time in milliseconds parse() = parses a date string and returns milliseconds UTC() = returns milliseconds in UTC Get Methods (getDay, getMonth, getHours, etc) Set Methods (setFullYear, setMinutes, setSeconds, etc) UTC Methods (getUTCDate, setUTCdate)
toString() - returns a string value of a date object
toDateString() - returns the date value of the date object without the time in a string format
toUTCString() - converts a date to a string using the UTC time zone
toISOString() - convert UTC time into a simplified ISO-8601 format
toLocaleDateString() - return a date value or parts of date in a specified locale
getDate / setDate
type coercion with different operators
== or !=
coerces primitives to numbers except for null and undefined
comparison operators
coerces primitives to numbers
logical operators
coerce primitives to boolean
type coercion with other primitives
symbol: evaluated as true in boolean coercion / cannot be implicitly coerced to a string or number
null: evaluated as false / 0 / null (string coercion)
undefined: false / NaN in numeric / undefined in string
NaN: false / NaN in string coercion
falsy values
false 0 -0 0n (BigInt 0) '' null undefined NaN
double bang
coerces to the equivalent boolean value
for in loop
vs.
for of loop
for in loop - when concerned with index
for of loop - for simplicity when you are just trying to access the values
adding elements to arrays
push() - add one or more element to the end of an array; returns the new length of the array
unshift() - add one or more elements to the beginning of array; returns new length
splice() - allows modifying the contents of an array; used to add elements starting from a specific condition
removing array elements
pop() - can be used to remove the last element from an array, returns removed element
shift() - remove first element; returns removed element
splice() - used to remove or replace, returns an array containing the removed elements
SLICE()
used to make a copy of an array or a portion of an array; does not change original array
finding array elements
includes()
indexOf()
lastIndexOf() - returns the last index of the given element in an array
findIndex() - returns first index that satisfies the given condition
find() - returns the value of the first element that satisfies the condition; undefined otherwise
advanced data manipulation
map() - creates a new array based on a callback function that is invoked for every element of the original array
filter() - create a new array that contains elements which pass the given condition in a callback function
reduce() - uses the specified reducer function to return a single output value based on the reduction
flat() - used to create a new array containing all the sub-array elements concatenated into the original array recursively up to the specified path
every() - used to check whether all the elements of an array pass the given condition
some() - used to check if at least one element of an array passes the given condition in a callback function
spread / rest operator
…
spreads the elements of array instead of the array itself
in json…properties and any strings….
are enclosed within double quotes