Chapter Four Knowledge Points

The return value of the data type judged by 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 and Array return object and
use an undefined variable will report an error, but typeof detects a variable data type will not report an error, return undefined

Note: typeof can only distinguish between value types,


String splicing

Using the plus operator
The easiest way to connect strings is to use the plus operator.

The following code uses the plus operator to concatenate two strings.

var s1 = "abc" , s2 = "def";
console.log(s1 + s2);  //返回字符串“abcdef”

Use concat() method

Use the string concat() method to add multiple parameters to the end of the specified string. There is no limit to the type and number of parameters of this method. It will convert all the parameters into strings, and then connect them to the end of the current string in order and finally return the connected string.

The following code uses the concat() method to concatenate multiple strings together.

var s1 = "abc";
var s2 = s1.concat("d" , "e" , "f");  //调用concat()连接字符串
console.log(s2);  //返回字符串“abcdef”

The concat() method does not modify the value of the original string, and the operation is similar to the concat() method of the array.

Use join() method

Use join() method

In a specific operating environment, you can also use the join() method of an array to connect strings, such as HTML string output.

The following code demonstrates how to concatenate strings with the help of arrays.

var s = "JavaScript" , a = [];
for (var i = 0; i < 1000; i ++) {
    a.push(s);
var str = a.join("");
a = null;
document.write(str);

Logical operators in Js

There are three logical operators in JavaScript, && and, || or,! NOT. Although they are called logical operators, these operators can be applied to any type of value, not just Boolean values, their results It can also be of any type.


Guess you like

Origin blog.csdn.net/weixin_54163765/article/details/114854395