typeof判断的数据类型的返回值
//number
console.log(typeof 123) //number
console.log(typeof NaN) //number
console.log(typeof 'abc') //string
console.log(typeof true) //boolean
console.log(typeof undefined) //undefined
//object
console.log(typeof null) // object
console.log(typeof {}) //object
console.log(typeof []) //object
//function
console.log(typeof console.log) //function
console.log(typeof isNaN) //function
console.log(a); // error: a is not defined
console.log(typeof a) ; // undefined
null和Array返回object
使用一个未定义的变量会报错,但是typeof检测一个为定义变量数据类型不会报错,返回undefined
注意:typeof只能区分值类型、
字符串拼接
使用加号运算符
连接字符串最简便的方法是使用加号运算符。
下面代码使用加号运算符连接两个字符串。
var s1 = "abc" , s2 = "def";
console.log(s1 + s2); //返回字符串“abcdef”
使用concat()方法
使用字符串 concat() 方法可以把多个参数添加到指定字符串的尾部。该方法的参数类型和个数没有限制,它会把所有参数都转换为字符串,然后按顺序连接到当前字符串的尾部最后返回连接后的字符串。
下面代码使用 concat() 方法把多个字符串连接在一起。
var s1 = "abc";
var s2 = s1.concat("d" , "e" , "f"); //调用concat()连接字符串
console.log(s2); //返回字符串“abcdef”
concat() 方法不会修改原字符串的值,与数组的 concat() 方法操作相似。
使用join()方法
使用join()方法
在特定的操作环境中,也可以借助数组的 join() 方法来连接字符串,如 HTML 字符串输出等。
下面代码演示了如何借助数组的方法来连接字符串。
var s = "JavaScript" , a = [];
for (var i = 0; i < 1000; i ++) {
a.push(s);
var str = a.join("");
a = null;
document.write(str);
Js中的逻辑运算符
JavaScript中有三个逻辑运算符,&&与、||或、!非,虽然他们被称为逻辑运算符,但这些运算符却可以被应用于任意类型的值而不仅仅是布尔值,他们的结果也同样可以是任意类型。