JavaScript Tutorial

JS HomeJS EditorsJS How toJS Basic ProgramJS ConsoleJS VariableJS constJS Data TypesJS StringJS Type CastingJS KeywordsJS OperatorsJS Use StrictJS Escape SequencesJS Comments

JS Control Statements

JS ifJS if elseJS else ifJS BreakJS ContinueJS Switch Case

JS Looping

JS Loop ForJS Loop For InJS Loop WhileJS Loop Do While

JS Array

JS ArrayJS Array One DimensionalJS Array Multi DimensionalJS Array Associative

JS Functions

JS FunctionJS Function SelfJS Function call()JS Function apply()JS Call By ValueJS Call By ReferenceJS Function Closures

JS Scope

JS Scope GlobalJS Scope LocalJS Let

JS File Handling

JS File Handling

JS Error Handling

JS try catchJS throwJS finally

JS Select Particulars

JS getElementByID()JS getElementsByClassName()JS getElementsByTagName()JS getElementsByName()JS querySelector()JS querySelectorAll()

JS Popup

JS alert()JS confirm()JS prompt()

JS Events

JS EventsJS Media EventsJS Mouse EventsJS Keyboard EventsJS Form EventsJS Drag EventsJS Clipboard EventsJS Print EventsJS Animation EventsJS Transition EventsJS Server-Sent EventsJS Touch EventsJS Object Events

JS Methods

JS Array MethodsJS String MethodsJS Math MethodsJS Number MethodsJS Date MethodsJS Element MethodsJS Window MethodsJS Node MethodsJS Document MethodsJS Console MethodsJS Timing MethodsJS Navigator MethodsJS Miscellaneous Methods

JS Properties

JS Array PropertiesJS String PropertiesJS Number PropertiesJS MouseEvent PropertiesJS KeyboardEvent PropertiesJS AnimationEvent PropertiesJS TransitionEvent PropertiesJS Element PropertiesJS Window PropertiesJS Node PropertiesJS Document PropertiesJS Navigator PropertiesJS HashChangeEvent PropertiesJS Miscellaneous Properties

JS Regular Expressions

JS Regexp

JS Cookies

JS Cookies

JS Interview Programs

JS Common ProgramsJS Array ProgramsJS Converter ProgramsJS Star Patterns ProgramJS Number Patterns ProgramJS Alphabet Patterns Program

JS Reference

LEARN JSONLEARN AJAXJS KeycodeMIME TypesASCII Table

JavaScript const

JavaScript const

The const is a JavaScript keyword for defining variables that are prevented from being updated.

Note: The const does NOT define a constant value. It defines a constant reference to a value, i.e., we cannot change constant primitive values, but we can change the properties of constant objects.

Update const

Note: The following example will throw an error as we are trying to update the value which is declared with const keyword.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> const x = 1; x = x + 1; document.write("x = "+x); </script> </body> </html>

Re-Assign const

Note: The following example will throw an error as we are trying to re-assogm the value which is declared with const keyword.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> const x = 1; x = 1; document.write("x = "+x); </script> </body> </html>

Add / Change / Delete Property in Object const

Note: The following example will NOT throw an error as we are trying to change the properties of constant objects.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> const bike = {company:"Honda", model:"CBR 1000RR"}; //Add property bike.color="Red"; //Change property bike.model="Gold Wing"; //Delete property delete bike["company"]; document.write("I went to "+bike.company+" Showroom"); document.write("<br>I purchased "+bike.model); document.write("<br>Which is "+bike.color+" in color"); </script> </body> </html>

Trying to Re-Assign Object const

Note: The following example will throw an error as we are trying to change the object itself.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> const bike = {company:"Honda", model:"CBR 1000RR"}; bike = {company:"Harley Davidson", model:"Fat Bob"}; document.write("Company = "+bike.company); </script> </body> </html>

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author