在javascript语句中添加空字符串的用法是什么? [英] What is the usage of adding an empty string in a javascript statement
本文介绍了在javascript语句中添加空字符串的用法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到许多JavaScript语句中使用的空字符串(''
或)但不确定是什么是否代表。
I see an empty string (''
or ""
) used in many JavaScript statements but not sure what does it stand for.
例如 var field = current.condition_field +'';
有人可以澄清一下吗?
推荐答案
输入类型。
它将类型转换为字符串
如果变量 current.condition_field
不是字符串
类型,通过使用 +添加
运算符在结尾/开头将其转换为''
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 + '';
所以,字段
总是字符串
。
示例
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);
感谢@KJPrice:
Thanks to @KJPrice:
当你想调用一个
字符串
方法(在字符串原型上定义的方法$ c $)时,这个特别有用。 c>)关于该变量。
This is especially useful when you want to call a
string
method(Method defined onstring prototype
) on that variable.
(myVar + '').toLowerCase();
这篇关于在javascript语句中添加空字符串的用法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文