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 ...
随机推荐
- openstack配置注意事项(主要是网络相关)
vlan协议应该配置为802.1q,另一个容易混淆的为ISL,配置命令为 switchport trunk encapsulation dot1q /islswitchport mode trunk ...
- Initialization and Class loading - Java
可以说,类的代码在初次使用时才加载.这通常指加载发生于创建类的第一个对象之时,但当访问 static域或static方法时,也会发生加载(通过下面的这段代码验证). class LoadTest { ...
- javascript中 的 + RegExp['\x241'] 怎么理解
\x24是十六进制转义符,16*2+4=36,ASCII码36代表的正是“$”符号(可以查ASCII码表),十六进制转义符的一般形式是'\xhh',h是0-9或A-F内的一个.$1是javascrip ...
- Maven下使用Jetty进行Debug
1.环境和条件 Maven-3.0.3Eclipse请阅读者事先具备一定maven知识 2 配置2.1 添加Jetty插件 在pom.xml中加入如下代码段 <plugin> <gr ...
- HDU 4726 Kia's Calculation(贪心构造)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...
- [HIHO1260]String Problem I(trie树)
题目链接:http://hihocoder.com/problemset/problem/1260 n个字符串,m次询问.每次询问给一个字符串,问这个字符串仅可以在一个地方加一个字母.这样操作后与n个 ...
- ViewPager的监听事件失效
主要是因为在我项目使用了PageIndicator,所以这个时候监听事件要写在PageIndicator上. mIndicator.setOnPageChangeListener(new OnPage ...
- bzoj2119
题意就是差分后求形如ABA的串的个数,B的长度为M 这是2011国家集训队互测的试题,是道好题,我直接给出出题人的题解吧: 对于这种在线性序列上的组合计数问题,我们很容易想到一个工具:分治!分治算法在 ...
- JSP:include的flush属性的作用
JSP 中include 另一个文件时有个很偏的属性,叫flush,默认为 false. 在同一个 JSP 中,如果不断 include 自己(源文件),在逻辑上会形成死循环.若默认情况下,服务器 ...
- UVa 11825 (状压DP) Hackers' Crackdown
这是我做状压DP的第一道题,状压里面都是用位运算来完成的,只要耐下心来弄明白每次位运算的含义,还是容易理解的. 题意: 有编号为0~n-1的n台服务器,每台都运行着n中服务,每台服务器还和若干台其他服 ...