Home > Web Front-end > JS Tutorial > Detailed analysis of JavaScript variable types_javascript skills

Detailed analysis of JavaScript variable types_javascript skills

WBOY
Release: 2016-05-16 15:50:46
Original
1623 people have browsed it

Variable type                                   

There are only 6 types: four primitive data types: boolean, number, string, undefine, other object and function are objects

typeof,instanceof

See the example directly:

    var obj = null;
    console.info(typeof obj);    //Object
    var arr = [];
    console.info(arr instanceof Object);  //true
    console.info(arr instanceof Array);  //true

Copy after login

Wapper Object of primitive data type

String, number, and boolean all correspond to specific packaging objects


Data type conversion

Use parseInt, parsetFolat to convert to numeric type


console.log(parseInt("34", 10)); //34
console.log(parseInt("34s5b", 10)); //34
console.log(parseInt("s", 10)); //NaN
console.log(parseInt(3.14, 10)); //3
Copy after login
JavaScript is a dynamically typed programming language. For the same variable, whatever type of data is captured is the same variable type

//number
var value = 100;
//string
value = "qiu";
//object
value = [1, 'two', 3];

Copy after login

"="Various patterns of numbers:

                                                                                                                                                                                                      because                                                                                                                                                                                                                                  === Strict judgment and other <严>




undefined vs null
var x = 42;
var y = "42";
console.log(x == y) //true;
console.log(x === y) //false
Copy after login

Udefine: means the variable is undefined and does not have a valid value (there is no variable yet, so how can we talk about the value)                     null:       nothing, a variable does not refer to any object. null is an object, type object (has variables, but does not reference values)


The judgment of undefine and null
var obj = null;
if (obj === null) {
alert("obj === null"); //此句将执行
}
else {
alert("obj!=null");
}
alert(typeof obj); //object

Copy after login

true and false
var myVar;
//true
console.log(typeof myVar === "undefined");
console.log(myVar === undefined);
var myVar2 = null;
console.log(typeof myVar2); //object
//true;
console.log(myVar2 == null);
console.log(myVar2 === null);
//true
console.info(myVar == myVar2); //undefine == null; 是true
//false
console.info(myVar === myVar2); //undefine === null; 是false
Copy after login

undefined, null,NaN,"",0 Except for these values, other values ​​are true;


Operator: ! ! with ||

                                !! Convert the following expression into a boolean value and return true or false                   !! "qiu"                                                                                                                                                                                                                                                                                                                                                                                                                                             var ns = ns || {}
If ns is undefined, return {}, otherwise return ns



Note: You must use var to define variables, otherwise you will get stuck! If you don’t write var, it will become a global variable

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template