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 ...
随机推荐
- Win XP 如何禁用系统的自动更新
想关闭系统的自动更新. 打开[控制面板]/[安全中心],发现“自动更新”和“更改安全中心通知我的方式”,都已成了灰色,无法更改. 网上查了一下,找到了这样一个处理方法:将[服务]中一个名为“Autom ...
- hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1240 开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向. 第一维代表在 ...
- Mac 切换Windows 使用虚拟机, 不推荐双系统
为什么使用虚拟机而不是双系统? 1.虚拟机可以随时在两个系统之间进行切换,便于在工作时使用而不影响效率.如果是双系统,在切换到另一个系统时需要关机重启,太过麻烦. 2.虚拟机除了运行Windows之 ...
- MVC项目中应用富文本编辑器UEditor中的几个坑
UEditor:百度出品 官网连接:http://ueditor.baidu.com/website/ 错误现象:在官网上复制到本地后,上传图片功能不能用, 控制台提示:“请求后台配置项http错误, ...
- DOM应用
父级.removeChild(子节点);父级.appendChild(子节点);父级.insertBefore(子节点, 在谁之前); 创建元素: <script> window.onlo ...
- pandas
- myisam 与innodb的区别
myisam 与innodb的区别 frm结构 fri索引 frd数据 innodb:一个表一个文件:frm文件 所有的innodb表,都使用表空间储存, 数据和索引的保存文件不同,myisam 分开 ...
- kdtree备份
库在这里 这个很好用. 例子: /*! gcc -Wall -g -o test test.c libkdtree.a */ #include <stdio.h> #include < ...
- mysql now() sysdate() curdate()区别
//mysql中now,curdate,curtime,sysdate区别 1.now 返回的时间,格式如下:2013-01-17 10:57:13 mysql> select now(),sl ...
- ECSide标签属性说明之<ec:column>
<ec:column>标签 ◆ 属性: columnId描述: 单元格的id,相当于<td>的id属性 ◆ 属性: title描述: 列在列表表头里显示的名称. ◆ 属性: p ...