Details of the five data types in Javascript

  • 2020-05-09 18:06:43
  • OfStack

Undefined

Undefined. There's only one value, undefined

Null

              has only one value, null

Boolean
In javascript, this is true as long as the logical expression does not return undefined or null.


if(3) true
if(null) false
if(undefined) false

Number

String

The char type does not exist in javascript.

String definitions can be in single or double quotation marks.


<html>
<head>
<script type="text/javascript">
//var s="hello";
//alert(typeof s);//s It's a string var s=new String("hello");//s It's an object type
alert(typeof s);
</script>
</head>        
<body>
</body>
</html>

typeof is a 1 meta operator used to get the data type of a variable
There are five return values for undefined,boolean, number,string, and object.

The first four are easy to understand. The last object is unknown to the programmer, and only returns object in general

In javascript, if the function does not declare a return value, undefined is returned by default.
If the return value is declared, then what is actually returned is what it is.

undefined is derived from null, so true is returned for comparison
                            alert(undefined==null);//true

Cast type conversion
In javascript, there are three casts:

Boolean(value)

Number(value)

String(value)


<html>
<head>
<script type="text/javascript">
var num=Number(3);
alert(num);
var s="hello";
alert(Boolean(s));
var s1=String("hello");
alert(typeof s1);
var obj=new String("hello");// This is not a cast!
alert(typeof obj);
</script>
</head>        
<body>
</body>
</html>

In javascript, all objects inherit from Object objects.

Generated by new.

Some of the methods in js are enumerable and some are not.

Use the js built-in method to determine whether it can be enumerated.


<html>
<head>
<script type="text/javascript">
var object=new Object();
for(var v in object){
    console.log(v);
}
alert(object.propertyIsEnumerable("prototype"));// Returned to the false , means that there are no properties that can be enumerated, which also means that the corresponding properties of the child object can not be enumerated
</script>
</head>        
<body>
</body>
</html>

Enumerate the properties of a custom type


<html>
<head>
<script type="text/javascript">
var member=function(name,age){
    this.name=name;
    this.age=age;
}
var m=new member("liudh",50);
for(var v in m){
    console.log(v);
    //name
    //age
}
alert(m.propertyIsEnumerable("prototype"));//false
//for(var v in window){
//    console.log(v);
//}
</script>
</head>        
<body>
</body>
</html>


Related articles: