JavaScript variables and primitive data types

Table of contents

1. Declare variables

(1)let

(2)const

(3) was

2. Basic type

(1) undefined and null

(2)string

(3) number and bigin

(4)boolean

(5)symbol


        JavaScript (JS) is a lightweight, interpreted or just-in-time compiled programming language with function-first features. Although it is well known as a scripting language in Web pages, it is also used in many non-browser environments, such as  Node.js , Apache CouchDB , Adobe Acrobat  , etc. Further, JavaScript is a prototype-based , multi-paradigm, single-threaded, dynamic language that supports object-oriented, imperative, and declarative (such as functional programming) styles.

1. Declare variables

(1)let

let 变量名 = 值;

Variables declared by let can be assigned multiple times, for example:

let a = 100;  // 初始值是 100
a = 200;	  // ok, 被重新赋值为 200

(2)const

The const modified is called a constant, which can only be assigned once

const b = 300; // 初始值是 300
b = 400;	   // error, 不能再次赋值

const does not mean that the content it refers to cannot be modified, for example:

const c = [1,2,3];
c[2] = 4; 	        // ok, 数组内容被修改成 [1,2,4]
c = [5,6];			// error, 不能再次赋值

(3) was

Variables declared by var can be assigned multiple times, for example:

var f = 100;
f = 200;

2. Basic type

(1) undefined and null

  • Execute an expression or function, no result is returned, and undefined appears

  • When accessing an element that does not exist in an array or an attribute that does not exist in an object, undefined appears

  • Define variable, not initialized, undefined appears

For example:

console.log(1);  	// 函数没有返回值, 结果是  undefined
let a = 10;		 	// 表达式没有返回值, 结果是 undefined
let b = [1,2,3];
console.log(b[10]); // 数组未定义元素是 undefined
let c = {"name":"张三"};
console.log(c.age); // 对象未定义属性是 undefined
let d;
console.log(d);		// 变量未初始化是 undefined

both have in common

  • There are no properties, methods

  • The two are collectively called Nullish

difference between the two

  • undefined generated by js

  • null provided by the programmer

(2)string

There are three ways to write js strings:

let a = "hello";  // 双引号
let b = "world";  // 单引号
let c = `hello`;  // 反引号

HTML code, compared with the expressions in java and js:

According to java, it is more cumbersome:

String s2 = """
    <a href="1.html">超链接</a>""";

js is more flexible:

let s1 = '<a href="1.html">超链接</a>';

let s2 = `<a href="1.html">超链接</a>`;

Template strings

Requirements: Request parameters for splicing URLs, such as:

/test?name=zhang&age=18
/test?name=li&age=20

Traditional method splicing:

let name = ; // zhang li ...
let age = ; // 18 20 ...

let uri = "/test?name=" + name + "&age=" + age;

Template string way:

let name = ; // zhang li ...
let age = ; // 18 20 ...

let uri = `/test?name=${name}&age=${age}`;

(3) number and bigin

The number type identifies double-precision floating-point decimals, for example:

10 / 3;   // 结果 3.3333333333333335

Since it is a floating-point decimal, it can be divided by zero:

10 / 0;	  // 结果 Infinity 正无穷大
-10 / 0;  // 结果 -Infinity 负无穷大

Floating-point decimals have operational precision issues, for example:

2.0 - 1.1; // 结果 0.8999999999999999

string to number

parseInt("10"); 	// 结果是数字 10 
parseInt("10.5");	// 结果是数字 10, 去除了小数部分
parseInt("10") / 3; // 结果仍视为 number 浮点数, 因此结果为 3.3333333333333335

parseInt("abc");	// 转换失败,结果是特殊值 NaN (Not a Number)

To represent a real integer, you need to use bigint, and the number ends with n to indicate that it is a bigint type

10n / 3n;			// 结果 3n, 按整数除法处理

(4)boolean

In js, not boolean can be used for conditional judgment, you can use [number], [string]... as judgment in the if statement

let b = 1;

if(b) { // true
    console.log("进入了");
}

At this time, there is a rule. When a conditional judgment is required, this value is regarded as true or false, and the value regarded as true is classified as truthy , and the value regarded as false is classified as falsy

The following values ​​are falsy:

  • false

  • Nullish (null, undefined)

  • 0, 0n, NaN

  • "" '' ``i.e. a string of length zero

Most of the remaining values ​​are truthy

There are a few that are easy to be regarded as falsy but are actually truthy

  • "false", "0"i.e. false for strings and zero for strings

  • []empty array

  • {}empty object

(5)symbol

Use less, you can refer to JavaScript at the following URL | MDN (mozilla.org) https://developer.mozilla.org/zh-CN/docs/Web/JavaScript

Guess you like

Origin blog.csdn.net/m0_61961937/article/details/130093524