Why are JavaScript primitive variables immutable? [duplicate] - javascript

This question already has answers here:
Understanding Javascript immutable variable
(6 answers)
Successfully changed the Immutable or Primitive Data-Types in JS. Then How these are Primitives or is JS Concepts wrong?
(3 answers)
How is mutability different from assigning a new value to a variable?
(2 answers)
Closed 1 year ago.
I am new to javascript and I am a little confused with the concept of immutability. Are javascript primitive variables immutable because they are accessed/ passed by value ? If this isn’t the reason, please mention.

Related

JavaScript changing a variable named const [duplicate]

This question already has answers here:
Keyword 'const' does not make the value immutable. What does it mean?
(5 answers)
Modifying a copy of a JavaScript object is causing the original object to change
(13 answers)
Closed 25 days ago.
I have an array declared with const, but when I console.log it 2 different times, it outputs different things.
Code where the variable is being logged, and declared
I looked at my code, and nothing should be modifying charSet, but it outputs different things.
The only thing that I can think of is that charSet is being changed because it is set = to charSet2

What does Object.constructor.constructor('alert(1)')() actually do in javascript? [duplicate]

This question already has answers here:
What's going on in this piece of Javascript?
(1 answer)
What is instance.constructor.constructor and how does it work?
(1 answer)
How is this function created? A = (0)['constructor']['constructor']
(1 answer)
Obfuscated Javascript in an exploit kit using Array's constructor
(2 answers)
Clever JavaScript to bypass eval method [duplicate]
(1 answer)
Closed 1 year ago.
I am involved in Application security and often times I've used
{{constructor.constructor('alert(1)')()}}
blindly in AngularJS applications to escape the sandbox in older versions. Recently I've been trying to understand what this really means and how it works. I tried reading about object constructors and understand that an object constructor points to the object type. so for example.
var a = new String;
console.log(a.constructor);
will print out String and if I do a.constructor.constructor this will print out {}
Now my question is how does object.constructor.constructor('ANY FUNCTION HERE')() lead to that function being executed in javascript

Does JavaScript create Objects of Variables I create ( Like be it String, Number etc. Except creating Object Variable itself) [duplicate]

This question already has answers here:
How is almost everything in Javascript an object?
(6 answers)
Why does a primitive variable act like an Object? [duplicate]
(3 answers)
javascript: do primitive strings have methods?
(2 answers)
Closed 2 years ago.
I have this question in mind since I started learning JavaScript last month.
What I have tried?: I researched for it online almost on all good sites but didn't get satisfactory answer in laymans language.
Question: When I create variable let suppose
let name = "mit"
name.toUpperCase()
I am using dot notation to access the method here and I know we use it for something in object. I was confused if the browser creates different object for name variable (which is of string data type here) or what?

Reason behind strange iterator syntax on Arrays [duplicate]

This question already has answers here:
What is the motivation for bringing Symbols to ES6?
(7 answers)
Why do you access Symbol.iterator via brackets?
(3 answers)
Closed 3 years ago.
I recently learned that to get an iterator from an Array, you have to access it using syntax that I have never seen before: let iterator = myArray[Symbol.iterator]()
It seems to me that implementing Array.prototype.getIterator() would have been a more idiomatic way to go, but I must be oversimplifying or just not understanding the significance of accessing this property of arrays in this particular way.
In attempting to deepen my understanding of the inner workings of JavaScript I was hoping someone could explain this diversion from a more traditional syntax.

Nested javascript invocation [duplicate]

This question already has answers here:
How does basic object/function chaining work in javascript?
(5 answers)
Understanding Methods Chaining in Javascript
(3 answers)
Closed 4 years ago.
So recently, i often to come across instagram post that invokes javascript i.e. like this --> foo.bar().baz().qux();. The insertion of parameters can be done on so many levels. However from what i have just learned, in nested object literal, we can only do foo.bar.baz.qux(); or only one parameter insertion. Can anyone show how to create nested object literals that can be invoked like this --> foo.bar().baz().qux(); ?
^cheers,

Categories

Resources