js data types include: String, Number, Boolean, Null, Undefined, Symbol, Object, Array, Function.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Javascript scripting language, like other languages, has its own basic data types, expressions and arithmetic operators, and the basic program framework of the program.
#What are the data types in js?
JavaScript data type:
Value type (basic type): String, Number, Boolean , for Null, Undefined, and Symbol.
Reference data types: Object, Array, Function.
Note: Symbol is a new primitive data type introduced in ES6 to represent unique values.
JavaScript has dynamic typing:
JavaScript has dynamic typing. This means that the same variable can be used as different types:
Instance
var x; // x 为 undefined var x = 5; // 现在 x 为数字 var x = "John"; // 现在 x 为字符串
JavaScript String
Strings are stored characters (such as " Bill Gates") variable.
The string can be any text in quotes. You can use single or double quotes:
var carname="Volvo XC60"; var carname='Volvo XC60';
You can use quotes within a string as long as they do not match the quotes surrounding the string:
var answer="It's alright"; var answer="He is called 'Johnny'"; var answer='He is called "Johnny"';
JavaScript Numbers
JavaScript has only one number type. Numbers can be written with or without a decimal point:
var x1=34.00; //使用小数点来写 var x2=34; //不使用小数点来写
Very large or very small numbers can be written using scientific (exponential) notation:
var y=123e5; // 12300000 var z=123e-5; // 0.00123
JavaScript Boolean
Boolean (logical) can only have two values: true or false.
var x=true; var y=false;
JavaScript Array
The following code creates an array named cars:
var cars=new Array(); cars[0]="Saab"; cars[1]="Volvo"; cars[2]="BMW";
or
(condensed array): var cars=new Array("Saab","Volvo","BMW");
or
(literal array):
var cars=["Saab","Volvo","BMW"];
Array subscripts are zero-based, so the first item is [0], the second is [1], and so on.
JavaScript Objects
Objects are separated by curly braces. Inside the parentheses, the object's properties are defined in the form of name and value pairs (name : value). Attributes are separated by commas:
var person={firstname:"John", lastname:"Doe", id:5566};
The object (person) in the above example has three attributes: firstname, lastname and id.
Spaces and line breaks don't matter. Declarations can span multiple lines:
var person={ firstname : "John", lastname : "Doe", id : 5566 };
There are two addressing methods for object properties:
name=person.lastname; name=person["lastname"];
Undefined and Null
Undefined This Value means the variable does not contain a value.
You can clear a variable by setting its value to null.
cars=null; person=null;
Declaring variable types
When you declare a new variable, you can use the keyword "new" to declare its type:
var carname=new String; var x= new Number; var y= new Boolean; var cars= new Array; var person= new Object;
More For related knowledge, please visit the FAQ column!
The above is the detailed content of What are the js data types?. For more information, please follow other related articles on the PHP Chinese website!