Easy
How do you declare an immutable (non-modifiable) variable in TypeScript?
Author: Vincent CotroStatus: PublishedQuestion passed 436 times
Edit
0
Community Evaluations
Incorrect answer
Jake08/11/2023
Values that are declared with const CAN be modified but they cannot be reinitialized or set to another variable. The moderators marked this as wrong, but it is CORRECT........ and it is easy to test!
Here is an example, tryin your code editor:
const arr = [1,2,3]
arr.push(4)
console.log(arr)
// You will see that the array indeed receives a new value.
// However, you cannot reinitialized the array. For example,
const newArr = [1];
newArr = [2] // This will throw an error. "Assignment to constant variable"
Jake
08/11/2023
My response is correct. Was this site built by non developers?
In typescript, try:
const arr = [1,2,3]
arr.push(4)
console.log(arr)
You will see that the array indeed receives a new value.
However, you cannot reinitialized the array. For example,
const arr = [1];
arr = [2] // This will throw an error. "Assignment to constant variable"
Similar QuestionsMore questions about Typescript
5
Write a function in TypeScript that takes a number as input and returns its square.5
Import an add function from a TypeScript module math.ts4
Which of the following utility types makes all properties of a given type optional?4
Define a custom type that represents a coordinate with an x and y property of type number in Typescript4
Create a generic reverseArray function to reverse the elements of an array while maintaining type safety.