JavaScript variables

The variable is the text assigned to the identifier, so you can refer to and use it later in the program. Learn how to declare one using JavaScript

Introduction to JavaScript variables

The variable is the text assigned to the identifier, so you can refer to and use it later in the program.

Variables inJavaScriptNo types are attached. After assigning a specific literal type to a variable, you can later reassign the variable to carry any other types without type errors or any problems.

This is why JavaScript is sometimes called "untyped".

The variable must be declared before it can be used. This can be done using the following three methodsvar,letorconst, These three methods are different in the way of interacting with variables in the future.

usevar

Until ES2015,varIt is the only construct that can be used to define variables.

var a = 0

If you forgot to addvarYou will assign a value to an undeclared variable, and the result may be different.

In a modern environment, after enabling strict mode, you will get an error. In older environments (or strict mode is disabled), this will initialize the variable and assign it to the global object.

If the variable is not initialized when the variable is declared, it will haveundefinedValue until you assign a value to it.

var a //typeof a === 'undefined'

You can declare a variable multiple times and overwrite it:

var a = 1
var a = 2

You can also declare multiple variables at once in the same statement:

var a = 1, b = 2

ThisrangeIt is the part of the code where the variable is visible.

Variable initialized tovarAny object outside the function is assigned to the global object, has a global scope, and is visible everywhere. Variable initialized tovarThe function assigned to the function inside the function is local, and is only visible inside the function, just like the function parameters.

Any variable defined in the function with the same name as the global variable will take precedence over the global variable and will be shaded.

It is important to understand that a block (identified by a pair of curly braces) does not define a new scope. The new scope is created only when the function is created, becausevarThere is no block scope, but there is a functional scope.

Inside the function, any variable defined in it is visible in the entire function code. Even if the variable is declared at the end of the function, it can still be referenced at the beginning, because JavaScript is actually before the code is executedMove all variables to the front(CalledHoisting). To avoid confusion, always declare variables at the beginning of the function.

uselet

letIs a new feature introduced in ES2015, essentiallyvar. Its scope is limited to the block, statement or expression that defines it, and all contained internal blocks.

Modern JavaScript developers may choose to only useletAnd give up completelyvar.

in caseletSeems to be an obscure term, just readlet color = 'red'AsMake the color redAll this makes more sense

definitionletBeyond any function-withvar-Do not create global variables.

usevarA global variable is defined at the top level, which is added to the browserwindowpurpose. A kindlet(withconst) A declaration outside the block will still create a variable that can be used in the application code, but is not assigned towindow.

useconst

Declared variablevarorletIt can be changed later in the program and then reassigned. onceconstAfter initialization, its value can no longer be changed, and it cannot be reassigned to another value.

const a = 'test'

We cannot assign other text toaconst. But we can mutateaIf it is an object that provides a method to change its content.

constNo immutability is provided, just to ensure that the reference cannot be changed.

constHas a block range, andlet.

Modern JavaScript developers may choose to always useconstThere is no need to reassign variables later in the program.

why? Because we should always use the simplest structure to avoid mistakes along the way.

Download mine for freeJavaScript beginner's manual


More js tutorials: