Skip to content

Instantly share code, notes, and snippets.

@khanh96le
Last active June 28, 2016 17:01
Show Gist options
  • Save khanh96le/8f52af5185afaf1a73ab05a8e6342151 to your computer and use it in GitHub Desktop.
Save khanh96le/8f52af5185afaf1a73ab05a8e6342151 to your computer and use it in GitHub Desktop.

#Values, Types, and Operator in JavaScript There are 6 basic types of value: number, string, Boolean, obeject, function, and undefined value

// NUMBER
console.log(2.99e9); 
// -> 2990000000 (2.99e9 = 2.99 x 10^9)
console.log(typeof NaN);
console.log(0 /0);
console.log(typeof Infinity);
console.log(1 /0);
console.log(typeof -Infinity);
console.log(-1 /0);
// Beside normal number like integer or float, there are 3 special types of number: Infinity, -Infinity, and NaN

// STRING
console.log("animals: " + "dogs " + "cats " + 'birds');

// UNARY and BINARY OPERATOR
console.log(typeof 1.23);
console.log(typeof "abc");  
// Binary operator operated on 2 values (such as: 1+1, 3*2, 4%1)
// Unary operator operated on only one value: (typeof is an unary operator)
// +,- is also binary and unary operator

// BOOLEAN 
console.log("Abc" < "Abcd") // -> true
console.log("abc" < "Abcd") // -> fasle
console.log("123" == "123") // -> true
console.log(NaN == NaN) // -> false

// UNDEFINED
console.log(typeof null);
console.log(typeof undefined);

// Automatic Conversion
console.log(8 * null); // -> 0
console.log("5" - 1); // -> 4
console.log("5" + 1); //-> 51
console.log(false == 0) //-> true
// Javascript automatically convert the wrong value to the true value of operator needs
// In case 1: operator * needs 2 numbers, so Javascript auto convert null to 0
// In case 2: operator - needs 2 numvers, so Javascript auto convert "5" to 5

// different between operator == and operator ===
console.log("5" == 5); // ->true
console.log("5" === 5); // ->false
// If we want to compare without automatically conversion then we use operator ===. The same for != and !==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment