Usage of let and const in javascript
let
/* var 可以重复声明 let 有块级作用域 没有前置功能 不能重复声明 */ var a=1; console.log(a);//1 let b=2; console.log(b);//2 if(a==1){ var z=2; } console.log(z);//2 /*if(a1==1){ var z1=2; } console.log(z1);//报错 a1 is not defined*/ for (var i = 0; i < 10 ; i++) { ; } console.log(i);//10 /*for (let i = 0; i < 10 ; i++) { ; } console.log(i);//i is not defined*/ /*function fn(){ let c=1; console.log(c);//1 } fn() console.log(c);//报错*/ console.log(d); //var d=5;//5 //let d=5;//报错 d is not defined var d=5; console.log(5);//5 var d=6; console.log(6);//6 /*let d=5; console.log(5);// let d=6; console.log(6);//报错 重复声明 */
const
/* const 是用来声明常量 不可改变的 命名建议大写 不能重复命名 块级作用域 常量在声明的时候必须赋值 但是值是不可以改变的 */ var a =2; a=3; console.log(a);//3 /*const a =2; a=3; console.log(a);//报错 不能给常量赋值*/ const A =2; console.log(A);//2 /*const a //必须赋值 a=3; console.log(a);//报错*/ const D=[1,2,3]; D.push(4);//[1,2,3,4] //D=[1,2,3,4]; 报错 console.log(D);//[1,2,3,4]
The above is the detailed content of Usage of let and const in javascript. 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

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

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.

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.

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.

C++ syntax error: const references cannot be used with non-const definitions, how to solve it? When we use const references in C++ programming, we often encounter a problem, that is, const references cannot be used in conjunction with non-const definitions. This is a type of C++ syntax error. So how do we solve this problem during programming? Let’s explain it in detail below. 1. The definition of const reference In C++, a const reference refers to an immutable object or

C++ syntax error: const-modified member functions must declare const members, how to deal with it? In the C++ language, const is a very important keyword, which is used to modify certain variables, pointers, member functions, etc. For member functions, if it is modified with the const keyword, the value of the member variable cannot be modified inside the function body. However, if we do not add the const keyword in both the function declaration and definition, we will encounter a compilation error "The const-modified member function must be declared

llet, var, and const represent block scope variables, function scope variables, and constants respectively. Detailed introduction: 1. let, used to declare a variable in a block scope. A variable declared using let cannot be accessed before it is declared. This is the so-called temporary dead zone; 2. var, used to declare the key to a variable. Word, the declared variable is in function scope or global scope and is not restricted by block-level scope; 3. const, used to declare a constant. Once assigned, the variable cannot be reassigned. The value is after declaration. Cannot be modified etc.
