Skip to content

Instantly share code, notes, and snippets.

View amysimmons's full-sized avatar

Amy Simmons amysimmons

View GitHub Profile
// undefined, functions and symbols are excluded from objects

y = {a: 'hello', b: true, c: 123, d: undefined, e: () => {}, f: Symbol()}
JSON.stringify(y) // "{"a":"hello","b":true,"c":123}" 

// in an array, bad values are replaced with null 

y = [1, 'abc', undefined, () => {}, Symbol()]
String(null) // "null"

String(undefined) // "undefined"

String(true) // "true"

String(false) // "false"

String(1) // "1"
var x = new String("hello")
var y = "hello"

console.log(x)
// String {[[PrimitiveValue]]: "hello"}

console.log(y)
// hello
/*
Simlple values are always assigned by value-copy.
Simple values include null, undefined, string, boolean, number, symbol.
Here, a holds a copy of 1, and b holds a separate copy of 1. Thus when
b changes, a is not affected. 
*/

var a = 1;
var b = a;
@amysimmons
amysimmons / zero.md
Last active December 21, 2017 10:23
0 / 3 //0
0 / -3 //-0
(0 / -3).toString() //"0"

0 === -0 //true
0 == -0 //true
0 > -0 //false
-0 < 0 //false
1 / 0 //Infinity
-1 / -0 //Infinity
-1 / 0 //-Infinity
1 / -0 //-Infinity

Number.NEGATIVE_INFINITY //-Infinity
Number.POSITIVE_INFINITY //Infinity

1 / 0 === Number.POSITIVE_INFINITY //true
NaN === NaN //false
NaN == NaN //false

typeof NaN //"number"

/*
window.isNaN() will return true for actual NaN values *and* when 
the result is simply not a number.
*/
42.001.toFixed(0) //"42"
42.001.toFixed(1) //"42.0"
42.001.toFixed(2) //"42.00"
42.001.toFixed(3) //"42.001"

42.001.toPrecision(2) //"42"
42.001.toPrecision(3) //"42.0"
42.001.toPrecision(4) //"42.00"
42.001.toPrecision(5) //"42.001"

Immutable strings:

a = "hello";
a[2] = "Z";
a //"hello"

a.toUpperCase(); //"HELLO"
a //"hello"

Mutable arrays:

var a;
a //undefined
typeof a //"undefined"

b //Uncaught ReferenceError: b is not defined
typeof b //"undefined"