Home > Web Front-end > JS Tutorial > body text

Usage of let and const in javascript

巴扎黑
Release: 2017-07-23 10:11:29
Original
1411 people have browsed it

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);//报错 重复声明
*/
Copy after login

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]
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!