Primitive types
undefined string number boolean object symbol
null bigint (future)
undeclared
Objects
object function callable object array subtype
Operator typeof var v; typeof v; 'undefined'
v = "1"; typeof v; 'string'
v = 2; typeof v; 'number'
v = true; typeof v; 'boolean'
v = {}; typeof v; 'object'
v = Symbol(); typeof v; 'symbol'
typeof doesNotExists 'undefined'
var v = null; typeof v; 'object'
v = function() {}; typeof v; 'function'
v = []; // v = Array(); // v = new Array(); typeof v; 'object' Array.isArray() method determines whether the passed value is an Array.
var v = 44n; typeof v; 'bigint'
undefined vs undeclared
uninitialized Temporal Dead Zone (TDZ) Refers to the region before a variable (const let) is defined.
console.log(test1); // undefined var test1 = 'foo';
console.log(test2); // ReferenceError: cannot access uninitialized variable const test2 = 'foo';
console.log(test3); // ReferenceError: cannot access uninitialized variable let test3 = 'foo';
Special Values
NaN -> invalid number
var myAge = Number("0o46") // 38
var myNextAge = Number("39"); // 39
var myCatAge = Number("n/a") // NaN
myAge - "My son age"; // NaN
Nan not identity value myCatAge === myCatAg // false
isNaN(myAge) // false
isNaN(myCatAge) // true
isNaN("My sons age") // true OOPS!
reason because its Coercion to number
Preferred way use Number Rescue Number.isNaN(myCatAge); // true Number.isNaN("My sons age") // as expected false
Negative Zero
var trendRate = -0;
trendRate === -0 // true
trendRate.toString() // "0" OOPS! trendRate === 0; // true OOPS!
trendRate < 0 // false HOW? trendRate > 0 // false
Rescue Object.is(trendRate, -0) // true Object.is(trendRate, 0) // false
Built in Objects Native Functions Built new Number() or new String()
Use new:
- Object()
- Array()
- Function()
- Date()
- RegExp()
- Error()
Don't use with new
- String()
- Number()
- Boolean()
var yesterday = new ("March 6, 2019");
yesterday.toUTCString(); // "Wed, 06 Mar 2019 06:00:00 GMT"
var myGPA = String(transcript.gap);
Abstract Operations
Type Conversion
toString()
null => "null" undefined => "undefined" true => "true" false => "false" 3.14159 => "3.14159" 0 => "0" -0 => "-0"
[] Array to string returns "" [] undefined,null value get remove and multiple values get , separated ["",false, undefined, null,,,,,0,-0,,-1,NaN].toString(); // ',false,,,,,,,0,0,,-1,NaN' Value of on empty ["",false, undefined, null,,,,,0,-0,,-1,NaN].valueOf() // ['', false, undefined, null, empty × 4, 0, -0, empty, -1, Na
{}.toString() [object,Object]
ToNumber "" => 0 "0" => 0 "-0" => -0 " 0009 " => 9 // remove white space and leading zero "0." => 0 "." => NaN "0xaf" => 175
false 0 true 1 null 0 undefined NaN
To Boolean is table lookup of falsy value "" 0, -0 null NaN false undefined