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 ...
随机推荐
- Friends(老友记)(六人行)相关资源
迅雷账号:104303980 老友记 Friends 的所有种子: http://www.ttmeiju.com/meiju/Friends.html 老友记(friends)高清(720p)+字幕 ...
- installation and configuration of OpenCV4Android SDK
http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/O4A_SDK.html#running-opencv ...
- Ubuntu对FireFox安装flash插件
有时候我们需要在Ubuntu下采用手动安装一些软件,比如Firefox的Flash插件.Adobe® Flash® Player 是一款轻量级浏览器插件,具有丰富的 Internet 应用运行时间,提 ...
- UVa 10154 - Weights and Measures
UVa 10154 - Weights and Measures I know, up on top you are seeing great sights, But down at the bot ...
- 计算机术语install和setup的区别
作为安装程序的Setup文件是软件的开发者专门为其软件设计的.exe文件,是当前最为盛行的安装程序.在运行该Setup文件进行软件安装时,Setup除了进行复制.改名.解压和目录维护等基本安装工作外, ...
- 《OD学hadoop》第四周0716
7.16 一.回顾 二.HDFS Federation(联盟) Hadoop 2.2.0发布新特性 很多的大公司都在使用:BAT HDFS Federation + HDFS HA架构 互相隔开,但是 ...
- Charles是Mac的Fiddler抓包工具
windows下面我们经常使用 Fiddler 抓包工具进行代理等一系列操作.然而,在 Mac 下的 Fiddler 勉强能运行,但是其挫的都不想说它了.今天看到朋友推荐这款 Charles Mac下 ...
- 记一次高级java工程师职位的面试
阿里在业内做java方面的有关开发可谓是一流的.它给我的第一印象,就是办事效率很高. 周日简历发过去,周一电话就打过来了,接到电话后,面试官很客气,问现在方面吗,我说现在在上班,有点忙,然后和面试官约 ...
- UVa 11988 (数组模拟链表) Broken Keyboard (a.k.a. Beiju Text)
题意: 模拟一个文本编辑器,可以输入字母数字下划线,如果遇到'['则认为是Home键,如果是']'则认作End键. 问最终屏幕上显示的结果是什么字符串. 分析: 如果在数组用大量的移动字符必然很耗时. ...
- iOS 开发 中级:UIToolbar,UINavigationBar,UITabBar,UIBarButtonItem,UITabBarItem自定义方法总结
原文: http://blog.csdn.net/songrotek/article/details/8692866?utm_source=tuicool 对于UIToolbar,UINavigat ...