Interview-Based Concept (CONST)

Interview-Based Concept (CONST)

Concept of const is a little bit tricky which you should understand if you are a javascript developer. When anybody didn't want to override the code then we use const in JS, Let's understand with an example.

const a=10;

a=11; // I can't change that 

I can't again re-assign the value, it doesn't mean that I can't put the value inside it. for example, I have an object

const x={b:12, c:13, d:14};

In this object, we can easily change the inside values, update the values and insert the values as well. But we can't reassign with the same name.

x.b=22; // Its a valid statement

x.c=23; // Its a valid statement

x.e=22; // Its a valid statement

x.f=21; // Its a valid statement

All the above statements are valid and work without an error. I am updating and assigning new values in the object. But we can't do reassign it like this

var x=10; // Not Valid

let x=[]; // Not Valid

const x={} // Not Valid


These 3 above statements are not valid because we already declared the object with const which name is "x".

In the same case of an array, we can modify the array inside elements, push the elements, pop or delete element as well.

const momi=["first","second","third"];

momi.push('forth'); //vaid statement

Output: ["first", "second", "third", "forth"];

momi.pop(); //valid statement 

Output: ["first", "second", "third"];

When you're adding to an array or object you're not re-assigning or re-declaring the constant, it's already declared and assigned, you're just adding to the "list" that the constant points to

const just means the variable cannot be reassigned, not that you can't manipulate it.


Let see const declaration with a function, whats happen

const f=()=>{ } //Arrow function with name 'f'

var f={Hello}; //Not valid

f=30; //Not valid

const f=()=>{ } // Not valid

let f=()=>{ } //Not Valid

Here 'f' is pointing to a specific function we cant reassign.

Keep it in your mind, Javascript supports overriding, not overloading. 

?

Regards: Azeem Aleem



 



Sajjad Ali

Software Engineer | Frontend Developer | ReactJs & NextJs | Building Scalable, User-Centric Web Apps ??

2 年

Summary: const just means the variable cannot be reassigned, not that you can't manipulate it.

回复

要查看或添加评论,请登录

Azeem Aleem的更多文章

  • Stop Using Inline Styles in React js

    Stop Using Inline Styles in React js

    Always avoid passing inline Styles to any react components. It's a little tiny thing that causes a huge problem in our…

    28 条评论
  • React + Leaflet in web Applications

    React + Leaflet in web Applications

    We use a lot of mobile apps which are using map services. Same like google map, Uber, Careem and Bykea mobile…

    2 条评论
  • Difference Between Git & Github

    Difference Between Git & Github

    Git Git is a version control system for tracking changes in computer files/Your local system. But what is the version…

    1 条评论
  • How does JS Hoisting work?

    How does JS Hoisting work?

    Variable Hoisting is a by the default behaviour of javascript to compile the code in a specific manner. We mostly…

  • Can I use Multiple useEffects in a single Component?

    Can I use Multiple useEffects in a single Component?

    Using the useEffect hook we can inform the react that we need some data after component render. It can handle any side…

    4 条评论
  • REST API vs Web API

    REST API vs Web API

    What is the difference between API and REST API? Not all APIs are REST, but all REST services are APIs So let's start a…

    4 条评论
  • Pure and Impure Functions in React/JS

    Pure and Impure Functions in React/JS

    Pure Functions A Pure function is a function that does not modify any external variable. And the Impure function…

    8 条评论
  • What is Async/await in React

    What is Async/await in React

    "Await" only works inside the Async functions. As you know that asynchronous function tasks are never dependent on each…

    7 条评论

社区洞察

其他会员也浏览了