logologo
  • Dashboard
  • Features
  • AI Tools
  • FAQs
  • Jobs
  • Modus
logologo

We source, screen & deliver pre-vetted developers—so you only interview high-signal candidates matched to your criteria.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Certifications
  • Topics
  • Collections
  • Articles
  • Services

AI Tools

  • AI Interviewer
  • Xperto AI
  • Pre-Vetted Top Developers

Procodebase © 2025. All rights reserved.

Q: const?

author
Generated by
ProCodebase AI

17/11/2024

JavaScript

What is const?

In JavaScript, const is one of the three keywords—let, const, and var—that you can use to declare variables. The primary feature of const is that it defines a variable that cannot be reassigned. This does not mean that the value assigned to a const variable cannot change, especially if it’s an object or an array; rather, it ensures that the variable itself cannot be pointed to a different value once it's been initialized.

Example of const in Action:

const PI = 3.14; console.log(PI); // Output: 3.14 // PI = 3.14159; // This will throw an error: TypeError: Assignment to constant variable.

In the example above, trying to reassign PI will result in an error because const prevents variable reassignment after its initial declaration.

Mutability of Objects and Arrays:

Although you cannot reassign a const variable, you can still modify the properties of an object or elements of an array declared with const. Here's how:

const car = { make: "Toyota", model: "Camry" }; car.model = "Corolla"; // This is allowed console.log(car); // Output: { make: "Toyota", model: "Corolla" } const numbers = [1, 2, 3]; numbers.push(4); // This is allowed console.log(numbers); // Output: [1, 2, 3, 4]

In these examples, while you cannot reassign the variable car or numbers, you can change the details within the object or array itself.

Understanding Context in JavaScript

Context refers to the value of this, which is a critical concept in JavaScript. It helps determine how functions and objects behave and influences variable accessibility and scope in JavaScript.

Types of Context:

  1. Global Context: This is the default context that exists when you run your JavaScript code. In a browser, the global object is window, and variables declared with var, let, or const in the global scope can be accessed as properties of window.

    var globalVar = "I am global"; console.log(window.globalVar); // Output: "I am global"
  2. Function Context: When a function is called, a new context is created. Within this function, this refers to the object that called the function. If the function is called as a method, this refers to the object that the method is a property of.

    const person = { name: "John", greet: function() { console.log("Hello, " + this.name); } }; person.greet(); // Output: "Hello, John"
  3. Block Context: With the introduction of ES6, JavaScript supports block-level scope, which is applicable for variables declared with let and const inside braces like functions, loops, and conditionals.

    { const x = 10; console.log(x); // Output: 10 } // console.log(x); // This will throw a ReferenceError

Importance of const and Context

Using const improves code readability and maintainability by signaling to other developers (and your future self) that the variable should not be re-assigned. When used correctly, it can prevent bugs that stem from unintended variable reassignments.

Understanding context is equally vital because it plays an essential role in how you access and manipulate data in your JavaScript programs. Knowing when to use this, as well as understanding how closures and scope work, can enhance the robustness of your code.

By mastering both const and context in JavaScript, you'll write cleaner and more efficient code that is easier to debug and maintain.

Popular Tags

JavaScriptconstcontext

Share now!

Related Questions

  • How does call

    17/11/2024 | VanillaJS

  • What is the difference between let

    17/11/2024 | VanillaJS

  • and bind differ in JavaScript

    17/11/2024 | VanillaJS

  • What are closures and how do they help with data privacy in JavaScript

    17/11/2024 | VanillaJS

  • What is the difference between == and === in JavaScript

    17/11/2024 | VanillaJS

  • What is the difference between null and undefined in JavaScript

    17/11/2024 | VanillaJS

  • What is event delegation in JavaScript

    17/11/2024 | VanillaJS

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design