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'
JavaScript Type Conversion的更多相关文章
- error: expected constructor, destructor, or type conversion before '.' token
今天写代码是遇到这样一个问题error: expected constructor, destructor, or type conversion before '.' token:立马网上查,原来是 ...
- 【错误】expected constructor, destructor, or type conversion before '.' token - 第八个游侠的日志 - 网易博客
[错误]expected constructor, destructor, or type conversion before '.' token - 第八个游侠的日志 - 网易博客 [错误]expe ...
- Type Conversion
文章著作权归作者所有.转载请联系作者,并在文中注明出处,给出原文链接. 本文原更新于作者的github博客,这里给出链接. 什么是转换 转换是接受一个类型的值并使用它作为另一个类型的等价值的过程.转换 ...
- Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(一)
题外话:本篇是对之前那篇的重排版.并拆分成两篇,免得没了看的兴趣. 前言 在Spring Framework官方文档中,这三者是放到一起讲的,但没有解释为什么放到一起.大概是默认了读者都是有相关经验的 ...
- Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(二)
接前一篇 Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 本篇主要内容:Spring Type Conver ...
- delphi 10.1 berlin datasnap提交clientdataset.delta报:invalid variant type conversion(类型转换错误)问题的解决
delphi 10.1 berlin datasnap提交clientdataset.delta报:invalid variant type conversion(类型转换错误)问题的解决,需要打这个 ...
- 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 ...
- Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion
本篇太乱,请移步: Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 写了删删了写,反复几次,对自己的描述很不 ...
- [C++] Type Conversion(类型转换)
Type Conversion(类型转换) Two kinds of type conversion explict type conversion(显式类型转换) impict type conve ...
随机推荐
- Socket基础编程
地址结构sockaddr_in 其中包含:IP地址,端口号,协议族推荐使用sockaddr_in,而不建议使用sockaddrsockaddr_in与sockaddr是等价的,但sockaddr_in ...
- PHP,单项查询及多项查询
先封装对象class DBDA { public $host = "localhost"; //数据库地址 public $uid = "root"; //数据 ...
- POJ 2689 Prime Distance (素数+两次筛选)
题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...
- POJ 1903 & ZOJ 2469 & UVA 1326 Jurassic Remains (部分枚举)
题意:给定n个只有大写字母组成的字符串,选取尽可能多的字符串,使得这些字符串中每个字母的个数都是偶数.n<=24 思路:直接枚举每个字符串的选或不选,复杂度是O(2^n).其实还有更简便的方法. ...
- 《Linux内核设计与实现》读书笔记(十)- 内核同步方法【转】
转自:http://www.cnblogs.com/wang_yb/archive/2013/05/01/3052865.html 内核中提供了多种方法来防止竞争条件,理解了这些方法的使用场景有助于我 ...
- poj - 3723 Conscription(最大权森林)
http://poj.org/problem?id=3723 windy需要挑选N各女孩,和M各男孩作为士兵,但是雇佣每个人都需要支付10000元的费用,如果男孩x和女孩y存在亲密度为d的关系,只要他 ...
- [51NOD]大数加法(模拟)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1005 要处理符号,还要特别注意0和连续进位等情况.偷懒使用st ...
- windows 创建SSH Key
1. 安装git,从程序目录打开 "Git Bash" (百度或用这个连接http://pan.baidu.com/s/1dDJCx9n 下载) 2. 键入命令:ssh-keyge ...
- cs108 java 02
Eclipise 1. import, 所有的homework 是以 eclipse project directories 的形式. 所以要选择 “File –> Import “, Exis ...
- Mysql中的Prepared Statement与Stored Precedure学习
可以参考: http://stackoverflow.com/questions/196652/prepared-statement-vs-stored-procedure They are not ...