Home > Web Front-end > JS Tutorial > js data types in JavaScript

js data types in JavaScript

一个新手
Release: 2017-10-12 09:26:49
Original
1499 people have browsed it

1. Data and types
As the saying goes, birds of a feather flock together, and people are divided into groups; here, people and things are distinguished according to categories. The same goes for our data. Using computers, we can process numerical values, text, and various data such as graphics, audio, and video. Different data have their own types. This is the data type we are talking about.
JavaScript is a weakly typed language. We will see this sentence in various JS introductory books; the popular explanation is that when we define a variable in JavaScript, we will not preset what type of data is stored in the variable (please refer to js variables and java (Contrast), you can only use var to declare variables. At this time, you can adapt to changes by staying unchanged, and any type of data can be stored in this variable. Note: In JavaScript, variables don’t have categories, but data does.

2. JS data types

Let’s take a look at the data types in JS. The following data types are defined in js : Generally divided into basic data types (simple data types) and reference data types (complex data types). Today we introduce the following basic data types.

1. Number (numeric type)

JavaScript does not distinguish between integers and floating point numbers, and uniformly uses Number to represent numerical values.

321;  // 整数321
3.24 ;  // 浮点数 3.14
1.1314e5;  // 科学计数法表示 1.1314*100000,结果为113140
NaN ;    // NaN表示Not a Number,当无法计算结果时,使用NaN表示
Infinity ;  // 表示无限大,当数值超过了JavaScript的数值型能表示的最大范围时,使用Infinity表示
Copy after login

Numerical data is mainly used to perform four arithmetic operations. Numerical data can be directly used to perform four arithmetic operations. The rules are consistent with mathematics:

1+1; //  2
(3+5)*0.5;  //4
1/0 ;  // Infinity
0/0;  //NaN
1%2;   //1     %取余
Copy after login
## 2. String (string)

A string is any text enclosed in single quotes (') or double quotes (")


‘hello’ ; //  hello   文本
“world”;  // world 文本
Copy after login
Note: Single quotes or double quotes are not text content, they are just a way of expression.


3. Boolean (Boolean)

Boolean values ​​are also used. It is said that the logical type has only two values, true or false; when making logical judgments, it can only be a Boolean value (direct Boolean value or converted to a Boolean value)


##.
true ; //  这是一个true值
false;  //这是一个false值
1>2  ;  // 这是一个 false值
0<=1 ;   // 这是一个 true值
Copy after login
4. null and undefined

null means an empty value, which means it is "empty". There is only one value, null, and undefined also means nothing. , In many cases, null and undefined both represent "none" values, and it is of little significance to distinguish between them. In most cases, we should use null. Undefined is only useful when determining whether the function parameters are passed. However, the default value of uninitialized variables is undefined.


#
a = null ;    //  a里面的值为空
a = undefined;  // 不建议这么写, 此时和   a = null 几乎没什么区别
Copy after login
##Current usage,null means "no object", that is, there should be no value there.
(1) As a parameter of the function, it means that the parameter of the function is not an object.

(2) As an object prototype chain. End point.

##

Object.getPrototypeOf(Object.prototype);      //结果为 null
Copy after login

##undefined means "missing value", that is, there should be a value here, but there is still Not defined. (2) When the function is called, the parameter that should be provided is not provided, and the parameter is equal to undefined. .
(1) When the variable is declared but not assigned, it is equal to undefined.
(3) The object has no assigned attribute, and the value of this attribute is undefined.

(4) When the function does not return a value, it returns undefined by default. #

var i;
i // undefined
function f(x){console.log(x)}
f() // undefined
var  o = new Object();
o.p // undefined
var x = f();
x // undefined
Copy after login


The above is the detailed content of js data types in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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