ES6 JavaScript: Understanding Let & Const

  • Variables can be declared using const in the same way that var or let declarations are.
  • The const keyword turns a variable into a constant whose value cannot be changed.
  • The scoping rules for const variables are the same as those for let variables.
    For example
const num=10;
num=10;

It Will gives you the following error.

Assignment to constant variable.

const must use a single statement to declare and initialise const variables. It is not possible to declare and initialise objects separately. {alertError}

const blog={
    title:'Code Guru',
    description:"Lorem ipsum"
};
blog={}
Assignment to constant variable.

You can update the existing property of the const object, and also you can add a new property.

blog.title="New Code Guru";

const is block-level

function constDemo(){
    const outer=10;
    function inner(){
        outer=20;
    }
    inner();
}
constDemo();

above code will throw the error 'Assignment to constant variable.`

إرسال تعليق

Please do not post any spam link in the comment box😊

أحدث أقدم

Blog ads

CodeGuru