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'

JavaScript Type Conversion的更多相关文章

  1. error: expected constructor, destructor, or type conversion before '.' token

    今天写代码是遇到这样一个问题error: expected constructor, destructor, or type conversion before '.' token:立马网上查,原来是 ...

  2. 【错误】expected constructor, destructor, or type conversion before '.' token - 第八个游侠的日志 - 网易博客

    [错误]expected constructor, destructor, or type conversion before '.' token - 第八个游侠的日志 - 网易博客 [错误]expe ...

  3. Type Conversion

    文章著作权归作者所有.转载请联系作者,并在文中注明出处,给出原文链接. 本文原更新于作者的github博客,这里给出链接. 什么是转换 转换是接受一个类型的值并使用它作为另一个类型的等价值的过程.转换 ...

  4. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(一)

    题外话:本篇是对之前那篇的重排版.并拆分成两篇,免得没了看的兴趣. 前言 在Spring Framework官方文档中,这三者是放到一起讲的,但没有解释为什么放到一起.大概是默认了读者都是有相关经验的 ...

  5. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(二)

    接前一篇 Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 本篇主要内容:Spring Type Conver ...

  6. delphi 10.1 berlin datasnap提交clientdataset.delta报:invalid variant type conversion(类型转换错误)问题的解决

    delphi 10.1 berlin datasnap提交clientdataset.delta报:invalid variant type conversion(类型转换错误)问题的解决,需要打这个 ...

  7. java 反射 报错:Attempt to get java.lang.Integer field "..." with illegal data type conversion to int

    类: Integer id; 反射时: Field f = User.class.getDeclaredField("id"); f.setAccessible(true); in ...

  8. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion

    本篇太乱,请移步: Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 写了删删了写,反复几次,对自己的描述很不 ...

  9. [C++] Type Conversion(类型转换)

    Type Conversion(类型转换) Two kinds of type conversion explict type conversion(显式类型转换) impict type conve ...

随机推荐

  1. openfire插件开发入门1

    .案例插件的功能 这个插件很简单,就是在openfire Server启动时,和关闭时,在控制台打印出消息. 3.插件开发的目录结构设计 先来看一下当前openfire在eclipse中的目录结构: ...

  2. 在 MapPath 的 Path 参数中不允许出现“..”字符。

    找到IIS应用程序池,“设置应用程序池默认属性”->“常规”->”启用 32 位应用程序”,设置为 True. 这样我的问题就解决了..

  3. 树形结构部门的 sqlserver 排序

    树形结构部门的 sqlserver 排序 因为要实现部门排序功能,而且要考虑部门的层级,直接用 sql 排序是不行的,所以写个 sql function 来支持. 首先部门表:company CREA ...

  4. MySQL —— 程序连接时的驱动名称和URL

    CONNECTION_DRIVER :  com.mysql.jdbc.Driver  CONNECTION_URL :    jdbc:mysql://localhost/DB_NAME 

  5. django中的filter和get的区别 (MultipleObjectsReturned: get() returned more than one Publisher --)(DoesNotExist: Publisher matching query does not exist.)

    上面的例子中`` filter()`` 函数返回一个记录集,这个记录集是一个列表. 相对列表来说,有些时候我们更需要获取单个的对象, `` get()`` 方法就是在此时使用的: >>&g ...

  6. AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...

  7. python下载图片

    import re import  urllib.request   def getHtml(url): page = urllib.request.urlopen(url) html = page. ...

  8. OracleApps 什么是Back to Back Order?

    什么是Back to Back Order? 简单的说,B2B是我们从供应商那拿货,然后收到货后,再发运给客户.. B2B Flow B2B的例子 1.Item的定义 Item Should be c ...

  9. emplace_back减少内存拷贝和移动

    --------<深入应用C++11:代码优化与工程级应用>第2章使用C++11改进程序性能,本章将分别介绍右值引用相关的新特性.本节为大家介绍emplace_back减少内存拷贝和移动. ...

  10. POJ (线段树) Who Gets the Most Candies?

    这道题综合性挺强的,又牵扯到数论,又有线段树. 线段树维护的信息就是区间中有多少个人没跳出去,然后计算出下一个人是剩下的人中第几个. 我在这调程序调了好久,就是那个模来模去的弄得我头晕. 不过题确实是 ...