JavaScript Features

Page 1

Introduction ECMAScript 6 Code Examples Constants Constant as an Object Summary

Introduction

This set of short tutorials cover relatively new, seldom used, or unusual JavaScript features. Over time, more pages with more techniques will be added, such as how to use scientific notation, bitwise operators, left and right shifts. Each page covers one or two techniques.

This page discusses constants and constants which are assigned an object. First read a general description of a constant, with a short example. Second look at constants with objects. What happens when you assign an object to a constant, then try to change a property on the object? Constants were implemented with ECMAScript 6.

ECMAScript 6

ECMAScript 6, also called ES6, ECMAScript 2015 and JavaScript 6, was complete in June 2015. ES6 includes constants, block scoped variables and functions, expression bodies, statement bodies, Lexical this, default parameter values, rest parameters, the spread operator, template literals, extended literals, enhanced regular expressions, enhanced object properties, destructuring assignment, modules, classes, class inheritance, symbol types, for and of iterators, generators, map sets, typed arrays, new built-in methods, promises, internationalization and meta-programming.

This page just discusses constants.

Code Examples

This tutorial series assumes HTML markup, for tests, includes an element with id = "eDebug". As follows.

<div 
 id="eDebug" 
>

</div >

Save a reference of eDebug to variable eDebug. Variable eDebug simplifies displaying output to the Web page during testing.

eDebug = document.getElementById(
 'eDebug'
);

Display output to the Web page.

eDebug.innerHTML = "Test Output";

Constants

Constants are variables that receive a value once. The value of the variable can't change during program execution. Assign any valid JavaScript expression, including functions, to a constant.

Declare a constant with the keyword, const, an identifier and a value. Assign any legal JavaScript expression to the value of the constant.

The following line assigns 6, to the constant identifier N_2_TIMES_3.

const N_2_TIMES_3 = 6;

Once the constant's declared and assigned, the following line would generate an error.

N_2_TIMES_3 = 30;

Constant as an Object

The value of a constant object's properties can change. Create and assign an object to a constant as follows.

const C_OBJECT = {
 name:"ConstantObject",
 count:0
};

Modify Const Property Value

If the value of the constant's an object, properties on the object itself can change. Modify the value of a property of an object and no error's generated.

You can change the value of a property on a constant if the constant's an object. The following code modifies property, name, on constant object, C_OBJECT. C_OBJECT.name: ChangedObject, displays to the Web page's eDebug element.

var eDebug = null;

const C_OBJECT = {
 name:"ConstantObject",
 count:0
};

function startTest(){
 eDebug = document.getElementById(
  'eDebug'
 );
 
 C_OBJECT.name = "ChangedObject";
 
 eDebug.innerHTML = "C_OBJECT.name: "+C_OBJECT.name;
 
}

Modify Const Value

The following short listing generates an error. You can't modify the value of a constant, even if that value's an object.

function startTest(){

eDebug = document.getElementById(
'eDebug'
);

C_OBJECT.name = "ChangedObject";

eDebug.innerHTML = "C_OBJECT.name: "+C_OBJECT.name;

try{

 C_OBJECT = C_OBJECT = {
  name:"DifferentObject",
  count:1
 } ; 
 
}
catch(ex){

 eDebug.innerHTML += "<br/>"+ex.toString();
 
}

}

Summary

This set of short tutorials cover relatively new, seldom used, or unusual JavaScript features. Over time, more pages with more techniques will be added, such as how to use scientific notation, bitwise operators, left and right shifts. Each page covers one or two techniques.

This page discussed constants and constants assigned an object. Declare a constant with the keyword, const, an identifier and a value. Declare a constant and assign an object to the constant. You can change the value of properties on the constant's object. You can't change the value of the constant itself.

Tags
JavaScript, canvas drawing, web developer, web design, website design, web designing course, how to become a web developer, coding websites, website developers,learning web design,html web design, html5 canvas tutorials, coding Website, html5 canvas, learn to code, javascript tutorial,html5 canvas tutorial,learn html tutorial, simple html tutorial
Copyright © 2015 Seven Thunder Software. All Rights Reserved.