admin管理员组文章数量:1434900
I see an empty string (''
or ""
) used in many JavaScript statements but not sure what does it stand for.
e.g. var field = current.condition_field + '';
Can someone please clarify?
I see an empty string (''
or ""
) used in many JavaScript statements but not sure what does it stand for.
e.g. var field = current.condition_field + '';
Can someone please clarify?
Share Improve this question edited Jun 23, 2015 at 2:47 Tushar 87.3k21 gold badges163 silver badges181 bronze badges asked Jun 22, 2015 at 13:15 user1934643user1934643 1612 silver badges11 bronze badges 1-
5
Number to String;
(""+5) + 1 == 51
– Alex K. Commented Jun 22, 2015 at 13:17
1 Answer
Reset to default 13Type Casting.
It converts the type to string
If variable current.condition_field
is not of string
type, by adding ''
using +
operator at the end/beginning of it converts it to string
.
var field = current.condition_field + '';
So, field
is always string
.
Example
var bool = true; // Boolean
var str = bool + ''; // "true"
document.write('bool: ' + typeof bool + '<br />str: ' + typeof str);
var num = 10; // Numeric
var str = num + ""; // "10"
document.write('<br /><br />num: ' + typeof num + '<br />str: ' + typeof str);
Thanks to @KJPrice:
This is especially useful when you want to call a
string
method(Method defined onstring prototype
) on that variable.
(myVar + '').toLowerCase();
本文标签: What is the usage of adding an empty string in a javascript statementStack Overflow
版权声明:本文标题:What is the usage of adding an empty string in a javascript statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745644966a2668080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论