JavaScript Type Conversion

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'

转载于:https://www.cnblogs.com/lilixu/p/4588038.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值