


Let's talk about the differences between var, let and const (code example)
本篇文章给大家带来了关于JavaScript的相关知识,其中主要给大家介绍了var、let以及const的区别有哪些,还有ECMAScript 和 JavaScript的关系介绍,感兴趣的朋友一起来看一下吧,希望对大家有帮助。
首先,ECMAScript 和 JavaScript 什么关系?
- ECMAScript是一个国际通过的标准化脚本语言。
- JavaScript由ECMAScript和DOM、BOM三者组成。 可以简单理解为:ECMAScript是JavaScript的语言规范,JavaScript是ECMAScript的实现和扩展。
1. var声明变量存在变量提升
,let和const不存在变量提升
console.log(a); // undefined ===> a已声明还没赋值,默认得到undefined值 console.log(b); // 报错:b is not defined ===> 找不到b这个变量 console.log(c); // 报错:c is not defined ===> 找不到c这个变量 var a = 100; let b = 10; const c = 10; console.log(a);//a=100
if(1){ var a = 100; let b = 10; const c = 1; } console.log(a); // 100 console.log(b) // 报错:b is not defined ===> 找不到b这个变量 console.log(c) // 报错:c is not defined ===> 找不到c这个变量
3. 同一作用域下let和const不能声明同名变量,而var可以
var a = 100; console.log(a); //控制台输出 100 var a = 10; console.log(a); //控制台输出 10 let a = 100; let a = 10; // 控制台报错:Identifier 'a' has already been declared ===> 标识符a已经被声明了。
4. const定义常量,而且不能修改,但是在定义的对象时对象属性值可以改变
const a=2 a=3 console.log(a) //控制台报错
const person = { name : 'make', sex : '男' } person.name = 'test' console.log(person.name) //运行发现控制台没有报错,且 person.name 被成功修改
????这是怎么回事呢?
因为对象是引用类型的,person中保存的仅是对象的指针,而修改对象的属性不会改变对象的指针,所以这种情况就会修改成功。也就是说const定义的引用类型只要指针不发生改变,都是被允许的。
接下来我们试着修改一下指针,让person指向一个新对象,最后果然报错
const person = { name : 'make', sex : '男'}person = { name : 'test', sex : '男'}console.log(person.name) //控制台报错
var定义的变量,变量提升,没有块的概念,可以跨块访问。
let定义的变量,只能在块作用域里访问,不能声明同名变量。
const用来定义常量,使用时必须初始化(即必须赋值),不能声明同名变量,只能在块作用域里访问,而且不能修改,但是在定义的对象时对象属性值可以改变。
他们都不能跨函数访问
推荐学习:《JavaScript视频教程》
The above is the detailed content of Let's talk about the differences between var, let and const (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

const is a keyword that can be used to declare constants, const modifiers in function parameters, const modified function return values, and const modified pointers. Detailed introduction: 1. Declare constants. The const keyword can be used to declare constants. The value of the constant cannot be modified during the running of the program. The constant can be a basic data type, such as integer, floating point number, character, etc., or a custom data type; 2. The const modifier in the function parameters. The const keyword can be used in the parameters of the function, indicating that the parameter cannot be modified inside the function, etc.

Audio output and input require specific drivers and services to work as expected on Windows 11. These sometimes end up running into errors in the background, causing audio issues like no audio output, missing audio devices, distorted audio, etc. How to Fix Audio Service Not Responding on Windows 11 We recommend you to start with the fixes mentioned below and work your way through the list until you manage to resolve your issue. The audio service may become unresponsive for a number of reasons on Windows 11. This list will help you verify and fix most issues that prevent audio services from responding on Windows 11. Please follow the relevant sections below to help you through the process. Method 1: Restart the audio service. You may encounter

This article brings you relevant knowledge about JavaScript. It mainly introduces the differences between var, let and const, as well as the relationship between ECMAScript and JavaScript. Interested friends can take a look at it. I hope Helpful to everyone.

Correct usage of the const keyword in C++: Using const to modify a function means that the function will not modify the parameters or class members passed in. Using const to declare a function pointer means that the pointer points to a constant function.

The role and examples of var keyword in PHP In PHP, the var keyword is used to declare a variable. In previous PHP versions, using the var keyword was the idiomatic way to declare member variables, but its use is no longer recommended. However, in some cases, the var keyword is still used. The var keyword is mainly used to declare a local variable, and the variable will automatically be marked as local scope. This means that the variable is only visible within the current block of code and cannot be accessed in other functions or blocks of code. Use var

The differences and characteristics of let, var and const: What do they mean? In JavaScript, let, var, and const are keywords used to declare variables. Each of them has different differences and characteristics. let: The let keyword was introduced in ES6, which allows us to declare a block-scoped variable. Block-level scope means that the variable is only visible in the block where it is declared and will not be promoted to the function scope. Example code: functionexampleFunctio

For C++ programmers, syntax errors are one of the most common problems. One of the common mistakes is that const objects must be initialized at definition time. If you encounter this situation, how should you deal with it? First, we need to understand what a const object is. The const keyword is a special type qualifier in C++ that specifies that the value of a variable cannot be changed during the execution of the program. Such variables are called "constants". If you define a const object without initializing it, you will encounter the above error. This is
