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