var and const in javascript code example
Example 1: what is const in javascript
const age; // errror as const cannot be kept un-initialized;
const age = 20 ;
const age = 21 , // error as once declared const variable cann't be
// re-declared in same scope or different scope.
Example 2: const in javascript
const value = 10;
const constant = value;
Example 3: javascript const
const b = 1; // this variable unique and can't change it.
Example 4: var or const in javascript
// var declares a variable, meaning its value will vary.
// const declares a constant, meaning its value will remain
// consistant and not change.
// If your variable changes throughout the program or website,
// declare it using a var statement.
// Otherwise, if its value does not change, declare it using
// a const statement.
const myConst='A const does not change.';
var myVar='A var does change.';
var myVar=2;
Example 5: js define constant by element id
const elem = document.getElementById('elementID');
Example 6: const js +=
const foo; // Errror as const cannot be kept un-initialized;
const foo = "foo";
console.log(foo); //Output: foo
foo += " bar"; //Error