The variable is the text assigned to the identifier, so you can refer to and use it later in the program.
The variable must be declared before it can be used. This can be done using the following three methods
const, These three methods are different in the way of interacting with variables in the future.
varIt is the only construct that can be used to define variables.
var a = 0
If you forgot to add
varYou 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 have
undefinedValue 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 to
varAny object outside the function is assigned to the global object, has a global scope, and is visible everywhere. Variable initialized to
varThe 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, because
varThere is no block scope, but there is a functional scope.
letIs a new feature introduced in ES2015, essentially
var. Its scope is limited to the block, statement or expression that defines it, and all contained internal blocks.
letAnd give up completely
letSeems to be an obscure term, just read
let color = 'red'AsMake the color redAll this makes more sense
letBeyond any function-with
var-Do not create global variables.
varA global variable is defined at the top level, which is added to the browser
windowpurpose. A kind
const) A declaration outside the block will still create a variable that can be used in the application code, but is not assigned to
letIt can be changed later in the program and then reassigned. once
constAfter initialization, its value can no longer be changed, and it cannot be reassigned to another value.
const a = 'test'
We cannot assign other text to
aconst. But we can mutate
aIf 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, and
constThere 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.
More js tutorials:
- The complete ECMAScript 2015-2019 guide
- Sample web application prompt list
- Introduction to Unicode and UTF-8
- Introduction to ES Module
- Introduction to CommonJS
- Moment.js tutorial
- ES6 guide
- ES2016 Guidelines
- ES2017 guide
- ES2018 guide
- How to use Async and Await in Array.prototype.map()
- Asynchronous and synchronous code
- HTML Canvas API tutorial
- What is a single page application?
- Introduction to WebAssembly
- Introduction to JSON
- JSONP guide
- Should you use or learn jQuery in 2020?
- Introduction to WebRTC library PeerJS
- Use Rest and Spread to process objects and arrays
- TypeScript guide
- Loosely typed and strongly typed languages
- The size of the node_modules folder is not a problem. This is a privilege
- String replace() method
- String search() method
- ES2019 guide
- String charAt() method
- String charCodeAt() method
- String codePointAt() method
- String concat() method
- String endsWith() method
- String include() method
- String indexOf() method
- String lastIndexOf() method
- String localeCompare() method
- String match() method
- String normalize() method
- String padEnd() method
- String padStart() method
- String repeat() method
- String slice() method
- String split() method
- String startsWith() method
- String substring() method
- String toLocaleLowerCase() method
- String toLocaleUpperCase() method
- String toLowerCase() method
- String toString() method
- String toUpperCase() method
- String trim() method
- String trimEnd() method
- String trimStart() method
- String valueOf() method
- Number isInteger() method
- Number isNaN() method
- Number isSafeInteger() method
- Number parseFloat() method
- Number parseInt() method
- Number toString() method
- Number valueOf() method
- Number toPrecision() method
- Number toExponential() method
- Number toLocaleString() method
- Number toFixed() method
- Number isFinite() method
- The assign() method of the object
- Object's create() method
- Object defineProperties() method
- Object defineProperty() method
- Object entry() method
- Object Frozen() method
- Object getOwnPropertyDescriptor() method
- Object getOwnPropertyDescriptors() method
- Object getOwnPropertyNames() method
- Object getOwnPropertySymbols() method
- Object getPrototypeOf() method
- Object is() method
- Object isExtensible() method
- Object isFrozen() method
- Object isSealed() method
- Object keys() method
- The preventExtensions() method of the object
- Object seal() method
- Object setPrototypeOf() method
- Object values() method
- Object hasOwnProperty() method
- Object isPrototypeOf() method
- The object's propertyIsEnumerable() method
- Object toLocaleString() method
- Object toString() method
- Object valueOf() method
- How to rename fields when using object destructuring
- How to send urlencoded data using Axios
- How to use extraction to upload files
- How to use top-level waits in ES modules
- How to send authorization header using Axios
- How to delete all node_modules folder contents
- How to get the first n items in an array in JS
- How to divide an array into multiple equal parts in JS
- How to load images in HTML canvas
- How to write text to HTML canvas
- How to fix TypeError: Cannot assign property "exports" error as read-only object "#<Object>"
- How to create an exit intent pop-up window
- How to check if an element is a descendant of another element
- How to force the use of credentials for every Axios request
- Gatsby, how to change the icon
- Use Gatsby to load external JS files
- Package, how to fix "regeneratorRuntime is not defined" error
- Object decomposition using types in TypeScript
- Deno Manual: A brief introduction to Deno🦕
- Event bubbling and event capture
- event.stopPropagation and event.preventDefault() and return false in DOM events
- In what ways can we access the value of an object property?
- What is the difference between method and function?
- The importance of timing when using the DOM
- Introduction to XState
- How to deal with promise rejection
- How to solve "cb.apply is not a function" error when using Gitbook
- Gatsby, fix "Cannot find module gatsby-cli/lib/reporter" error