Declaration

suggest change

There are four principle ways to declare a variable in JavaScript: using the var, let or const keywords, or without a keyword at all (“bare” declaration). The method used determines the resulting scope of the variable, or reassignability in the case of const.

var a = 'foo';    // Function-scope
let b = 'foo';    // Block-scope
const c = 'foo';  // Block-scope & immutable reference

Keep in mind that you can’t declare constants without initializing them at the same time.

const foo; // "Uncaught SyntaxError: Missing initializer in const declaration"

(An example of keyword-less variable declaration is not included above for technical reasons. Continue reading to see an example.)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents