Home > Common Problem > What are the js data types?

What are the js data types?

奋力向前
Release: 2021-09-30 15:13:04
Original
7996 people have browsed it

js data types include: String, Number, Boolean, Null, Undefined, Symbol, Object, Array, Function.

What are the js data types?

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 为字符串
Copy after login

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';
Copy after login

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"';
Copy after login

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;         //不使用小数点来写
Copy after login

Very large or very small numbers can be written using scientific (exponential) notation:

var y=123e5;      // 12300000
var z=123e-5;     // 0.00123
Copy after login

JavaScript Boolean

Boolean (logical) can only have two values: true or false.

var x=true;
var y=false;
Copy after login

JavaScript Array

The following code creates an array named cars:

var cars=new Array();
cars[0]="Saab";
cars[1]="Volvo";
cars[2]="BMW";
Copy after login

or

(condensed array):
var cars=new Array("Saab","Volvo","BMW");
Copy after login

or

(literal array):
Copy after login
var cars=["Saab","Volvo","BMW"];
Copy after login

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};
Copy after login

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
};
Copy after login

There are two addressing methods for object properties:

name=person.lastname;
name=person["lastname"];
Copy after login

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;
Copy after login

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;
Copy after login

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!

Related labels:
js
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template