JavaScript Value of variable not getting updated after assignment

var a = 2;
var b = a;

console.log( b ); //2

a= 5;

console.log( b ); //2

Q: Why variable 'b' is getting value 2 even when variable 'a' is assigned a different value

Answer:1

console.log(b) returns 2 because when you access a primitive type you work directly on its value.

Answer:2

Cause numbers are immutable.

Changing an immutable value, replaces the original value with a new value, hence the original value is not changed (thats why b = 2).

If you need a reference, use object and/or arrays var a ={value: 2}, b = a a.value = 3 // also changes the value of be, since it can mutate

Answer:3

In javascript, primitives (number, bool, string) are assigned by value, only objects are assigned by reference.

Answer:4

In Javascript, integers are immutable. It means that the object's value once assigned cannot change. When you do

a=5;
b=a;

It is true that both are names of the same object whose value is 5. Later when you do -

a=2

It assigns the reference a a new object whose value is 2. So essentially a now points to a new object. Ans both objects exist.

For a better understanding you can refer to this link

Answer:5

I'm creating React application that use JsSIP library to answer calls made via VoIP SIP provider. I've already created a page that have two buttons (Accept and Reject). It successfully register SIP ...

I'm creating React application that use JsSIP library to answer calls made via VoIP SIP provider. I've already created a page that have two buttons (Accept and Reject). It successfully register SIP ...

I'm trying to use the arr.includes(item). The function should return True if the item is an element of the array. But it doesn't seem to be able to do so with a multidimensional array. Take a look at ...

I'm trying to use the arr.includes(item). The function should return True if the item is an element of the array. But it doesn't seem to be able to do so with a multidimensional array. Take a look at ...

i read this question and this is very useful for me. Chrome desktop notification example? is there any way to customize Notification via HTML markup?? and custom CSS styling for that Notification var ...

i read this question and this is very useful for me. Chrome desktop notification example? is there any way to customize Notification via HTML markup?? and custom CSS styling for that Notification var ...

  1. desktop notification with javascript
  2. desktop notification for gmail
  3. desktop notification for pc
  4. desktop notification in php
  5. desktop notification for outlook
  6. desktop notification for android
  7. desktop notification in laravel
  8. desktop notification for chrome
  9. desktop notification for website
  10. desktop notification in python
  11. desktop notification for outlook web app
  12. desktop notification in codeigniter
  13. desktop notification for outlook 365
  14. desktop notification in asp.net mvc
  15. desktop notification in html5
  16. desktop notification from browser
  17. desktop notification for shared mailbox
  18. desktop notification for outlook mail
  19. desktop notification for whatsapp
  20. desktop notification from excel

For some reason my variable keeps resetting to 2 despite me wanting to increment the variable when a function is called. It's really annoying me and I've refactored the code several times to no avail! ...

For some reason my variable keeps resetting to 2 despite me wanting to increment the variable when a function is called. It's really annoying me and I've refactored the code several times to no avail! ...

  1. variable increment jquery
  2. increment variable value in jquery
  3. jquery variable increment by 1
  4. increment variable using jquery
  5. jquery increment variable name
  6. increment variable on click jquery
  7. jquery increment global variable
  8. jquery each increment variable