Home Common Problem What are the js data types?

What are the js data types?

Sep 30, 2021 pm 03:12 PM
js

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JS and Baidu Maps to implement map pan function How to use JS and Baidu Maps to implement map pan function Nov 21, 2023 am 10:00 AM

How to use JS and Baidu Maps to implement map pan function

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to create a stock candlestick chart using PHP and JS

How to use JS and Baidu Maps to implement map polygon drawing function How to use JS and Baidu Maps to implement map polygon drawing function Nov 21, 2023 am 10:53 AM

How to use JS and Baidu Maps to implement map polygon drawing function

How to use JS and Baidu Map to implement map click event processing function How to use JS and Baidu Map to implement map click event processing function Nov 21, 2023 am 11:11 AM

How to use JS and Baidu Map to implement map click event processing function

What does the new operator do in js What does the new operator do in js Nov 13, 2023 pm 04:05 PM

What does the new operator do in js