Data Types
5 Data Types
string, number, boolean, object, function
3 Object Types
object, array, date
2 Other Types
null, undefined
Type Conversion
Number/Boolean/Date -> String
String(x) // x can be any number, expression or variable
x.toString()
String/Boolean -> Number
Number(x)
Number(' ') => 0
Number('') => 0
Number('1 2') => NaN Number('1+2') => NaN
Number(false) => 0 Number(true) => 1
Operator '+'
+ can convert a variable to a number
+ '5' => 5
+ 'a' => NaN
Implicit Type Conversion
+ can also be applied in the cancatening the strings.
Infinity+(-Infinity) => NaN
+0+(+0) => +0, (-0)+(-0) => -0, (+0)+(-0) => +0
var c = a+b
if a is string:
if b is string: return the concatenation of a+b // 'x' + 'y' => 'xy'
if b is not string : return a+b.toString
if a is number:
if b is string: return a.toString+b // 100+'23' => '10023' '3' + 4 + 5 => '345' 3 + 4 + '5' => '75'
== will perform implict conversion on the varibale before comparing
string == number => Number(string) == number
boolean == ? => Number(boolean) == ?
object == ?(not obj) => valueof(object) == ?
null == undefined => return true
NaN == ? => return false // NaN == NaN => return false *NaN is unequal to every value including itself
object1 == object2 => compare if they point to the same object
Similiar with <, >, <=, >=
&& and ||
null && ? => null NaN && ? => NaN // if theie is a null/NaN/undefined evaulted value, return null/NaN/undefined
null && NaN => null NaN && null => NaN undefined && null => undefined
same as the cases in '||'
a && b
if a evalutes to be true: return b (original value before evalution)
else: return a (original value before evalution)
a || b
if a evalutes to be false: return b (original value before evalution)
else: return a (original value before evalution)
name = other_name || 'default'