Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Js Const in JavaScript

Js Const in JavaScript

? JavaScript const


What is const?

  • const declares a block-scoped constant variable.

  • The variable must be initialized when declared.

  • Its binding cannot be reassigned after initialization.


Basic Example

const PI = 3.14159;console.log(PI);  // 3.14159// Trying to reassign throws an error// PI = 3.14;  // TypeError: Assignment to constant variable.

Important Details

  • const means the binding (variable reference) is constant, not the value itself.

  • For objects and arrays, you can modify their contents, but cannot reassign the variable.

const arr = [1, 2, 3];arr.push(4);        // Allowedconsole.log(arr);   // [1, 2, 3, 4]// arr = [5, 6];    // Error: Assignment to constant variable.

const vs let vs var

KeywordScopeReassignableHoisting behavior
constBlock-scopedNoNo, not usable before declaration
letBlock-scopedYesNo, not usable before declaration
varFunction-scopedYesYes, undefined before assignment

When to Use const

  • Use const by default.

  • Use let only if the variable value will change.

  • Avoid var in modern JS.


Want me to show you examples of scope differences or how to safely use const with objects and arrays?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql