← Back to JavaScript Course | Chapter 1: Basics & Syntax | Lesson 9 of 12

JS Const

Think of a sealed shipping container, once it's packed and sealed at the factory, you can't swap out the whole container for a different one, but if it happens to be full of individually movable crates, you can still rearrange or open those crates inside. const works the same way for variables, once you declare a const, you can never reassign it to point at a different value entirely, but if that value is an object or array, its contents can still be changed. const protects the binding, not necessarily the whole value. On real projects like cookiescursor.com, defaulting to const wherever possible makes code easier to trust at a glance, because you immediately know that name will never point somewhere else.
Syntax
javascript
const CONSTANT_NAME = value;

Constant Values

const variable की binding declaration के क्षण ही जकड़ दी जाती है, यानी आप बाद में script में उसी variable नाम को किसी अलग value की ओर कभी इशारा नहीं करा सकते।

Note: किसी भी variable के लिए const को अपनी डिफ़ॉल्ट पसंद बनाइए, let पर केवल तब जाइए जब आपको पता चले कि value को सचमुच बदलना है।
Warning: Attempting to assign a new value to a const variable, even one of the same type, throws a TypeError immediately.

उदाहरण: Constant Values

javascript
const maxUsers = 100;
// maxUsers = 200; // TypeError: Assignment to constant variable
console.log(maxUsers);

const को Initialize करना ज़रूरी है

क्योंकि const variable का value declaration के बाद कभी बदल नहीं सकता, JavaScript आपसे वह value तुरंत देने की माँग करती है, बिना value के const घोषित करना अनुमत नहीं है।

Note: अगर आप बिना शुरुआती value के variable घोषित करना चाहते हैं, तो यह आमतौर पर संकेत है कि आपको असल में const की जगह let चाहिए।
Warning: Writing const withoutValue; on its own line causes a SyntaxError before the script even starts running.

उदाहरण: const Must Be Initialized

javascript
const pi = 3.14159;
// const withoutValue; // SyntaxError: Missing initializer in const declaration
console.log(pi);

Objects के साथ const

जब const variable किसी object को रखता है, तो variable खुद किसी नए object को दोबारा assign नहीं किया जा सकता, लेकिन उस object के भीतर की properties को फिर भी खुलकर जोड़ा, बदला या हटाया जा सकता है।

Note: object पर const एक वादा है कि यह variable हमेशा उसी object को दर्शाएगा, यह वादा नहीं कि object की सामग्री कभी नहीं बदलेगी।
Warning: Developers new to const are sometimes surprised that modifying an object's property doesn't throw an error, only reassigning the variable itself does.

उदाहरण: const With Objects

javascript
const user = { name: "Amit" };
user.name = "Priya"; // allowed, modifying a property
// user = {}; // TypeError, cannot reassign the variable
console.log(user);

Arrays के साथ const

const से घोषित arrays पर भी वही नियम लागू होता है, array के elements को array methods से जोड़ा, हटाया या बदला जा सकता है, लेकिन variable को कभी किसी पूरी तरह अलग array की ओर इशारा करने के लिए दोबारा assign नहीं किया जा सकता।

Note: push और splice जैसे methods मौजूदा array को वहीं बदलते हैं, इसीलिए वे const arrays पर ठीक काम करते हैं।
Warning: Using the assignment operator to replace an entire const array, like myArray = [], fails, even though modifying it with push does not.

उदाहरण: const With Arrays

javascript
const numbers = [1, 2, 3];
numbers.push(4); // allowed
// numbers = []; // TypeError, cannot reassign
console.log(numbers);

const कब इस्तेमाल करें

आधुनिक JavaScript में अधिकांश variables के लिए const डिफ़ॉल्ट पसंद होना चाहिए, इसका लगातार उपयोग code पढ़ने वाले किसी को भी ठीक बता देता है कि कौन से values स्थिर रहने के लिए हैं और कौन से, जिन्हें let से चिह्नित किया गया है, बदलने की उम्मीद की जाती है।

Note: नया code हर जगह const के साथ लिखने की कोशिश कीजिए, और विशिष्ट variables को let में केवल तब बदलिए जब code दोबारा assign किए बिना सचमुच न चले।
Warning: Overusing let purely out of caution, even for values that never actually change, makes it harder to tell at a glance which variables are truly dynamic.

उदाहरण: When to Use const

javascript
const taxRate = 0.08; // never changes
let price = 100;
console.log(price + price * taxRate);
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.