current position:Home>Front end - JS based variables

Front end - JS based variables

2022-01-26 22:15:04 Dunhuang Bi painting

Declare variable keywords

Variables are used to store data

You need to declare variables first , Can be used

There are three main keywords used to declare variables :let、const、var

var The key words are the old version (ES5)JS Used by the , Now it's out

let

let Is the keyword used to declare variables in the new version ,let The declared variables are ordinary variables

usage :

let a = 1

//  perhaps 

let a

a = 1

The above uses let Declare a variable , A variable called a, The value is 1

Use of variables

Proper use :

//  Declare variables first 【 This is an annotation. 】
let a = 1 //  Assign values when declaring 

//  Use variables after 
alert(a) //  The browser will pop up here :1

image-20210513193751186

The above code can also

let a // let Variables can be declared without assignment 

a = 1 //  The variable a Assign a value 

alert(a) //  Popup :1

Incorrect usage :

//  Use variables here first 
alert(a) //  An error will be reported here 
//  Make a statement after , Wrong usage , Because in use , Variable is not declared , So there's an error 
let a = 1

image-20210513193951341

Conclusion :

Variables must be declared before using !!!

First use , An error will be reported after the statement

Declaration of variables

In the same environment ( Within the same scope ) All variable names cannot conflict , Otherwise, the report will be wrong

let a = 1

//  Here are other codes 

let a = 1 //  An error will be reported here 

Conclusion :

Variables cannot be declared repeatedly within the same scope

The concept of scope will be explained in the following chapters

Change the variable value

let The value of the declared variable can be changed

let a = 1
alert(a) //  Pop up here :1

a = 2 //  The variable... Has been changed here a The value of is 2, Use later a when , The value is a
alert(a) //  Pop up here :2

Conclusion :

When changing the value of a declared variable , Just assign a value to the variable directly , There is no need to write a declaration keyword

const

const The declared variable is Constant , Instead of Common variables

Constant : A constant is a special variable , The value of this variable must be set at the time of declaration , And cannot be changed later .

const a = 1 //  declare constant a, The value is 1

a = 2 //  Modifying the value of constant here is an illegal operation , So there's an error 

image-20210513195403879

besides ,const The properties of constants are similar to let Variable consistency .

Keyword reserved word list

abstract arguments boolean break byte
case catch char class* const
continue debugger default delete do
double else enum* eval export*
extends* false final finally float
for function goto if implements
import* in instanceof int interface
let long native new null
package private protected public return
short static super* switch synchronized
this throw throws transient true
try typeof var void volatile
while with yield

Object and method

Array Date eval function hasOwnProperty
Infinity isFinite isNaN isPrototypeOf length
Math NaN name Number Object
prototype String toString undefined valueOf

window Keyword under object

alert all anchor anchors area
assign blur button checkbox clearInterval
clearTimeout clientInformation close closed confirm
constructor crypto decodeURI decodeURIComponent defaultStatus
document element elements embed embeds
encodeURI encodeURIComponent escape event fileUpload
focus form forms frame innerHeight
innerWidth layer layers link location
mimeTypes navigate navigator frames frameRate
hidden history image images offscreenBuffering
open opener option outerHeight outerWidth
packages pageXOffset pageYOffset parent parseFloat
parseInt password pkcs11 plugin prompt
propertyIsEnum radio reset screenX screenY
scroll secure select self setInterval
setTimeout status submit taint text
textarea top unescape untaint window

It should be noted that name This word , Not a keyword or reserved word , In most browsers and situations, it can be used as a variable , However, there may be very few and special cases of abnormality
| textarea | top | unescape | untaint | window |

It should be noted that name This word , Not a keyword or reserved word , In most browsers and situations, it can be used as a variable , However, there may be very few and special cases of abnormality

copyright notice
author[Dunhuang Bi painting],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262215009076.html

Random recommended