const
The keyword const is a little misleading.
It does not define a constant value. It defines a constant reference to a value.
Because of this you can NOT:
- Reassign a constant value
- Reassign a constant array
- Reassign a constant object
But you CAN:
- Change the elements of constant array
- Change the properties of constant object
// You can create a constant array:
const cars = ["Saab", "Volvo", "BMW"];
// You can change an element:
cars[0] = "Toyota";
// You can add an element:
cars.push("Audi");
structuredClone()
structuredClone() is a built-in JavaScript function that creates a deep copy of a value, allowing for the cloning of complex data types like objects, arrays, and even certain built-in types such as Map and Set. It does not support cloning functions or DOM nodes, and it was introduced to provide a more reliable alternative to JSON-based cloning methods.
const ironman = {
firstName: 'Tony',
lastName: 'Stark',
age: 45,
address: {
city: 'New York'
}
}
// It´s not a deep clone and works just a first level
const spiderMan = {... iroman};
const ironman = {
firstName: 'Tony',
lastName: 'Stark',
age: 45,
address: {
city: 'New York'
}
}
// Creates a deep clone, includes object address
const spiderMan = structuredClone(ironman);