The difference between readonly and const in Type Script

These two features are similar in that they are both non-assignable.

Can you explain exactly it?

In this article, I will share the differences between them.

const prevents reassignment to a variable.

In this case, hisName is a variable th…


This content originally appeared on DEV Community and was authored by Noah

These two features are similar in that they are both non-assignable.

Can you explain exactly it?

In this article, I will share the differences between them.

const prevents reassignment to a variable.

In this case, hisName is a variable that cannot be reassigned.

const hisName = 'Michael Scofield'

hisName = 'Lincoln Burrows'
// → ❌ Cannot assign to 'hisName' because it is a constant.

However, you can reassign to property.

const hisFamily = {
  brother: 'Lincoln Burrows'
}

hisFamily.brother = ''
// → ⭕️

hisFamily = {
  mother: 'Christina Rose Scofield'
}
// → ❌ Cannot assign to 'hisFamily' because it is a constant.

readonly prevents reassignment to a property.

For example, if you try to assign a value to brother with readonly, a compilation error will occur.

let hisFamily: { readonly brother: string } = {
  brother: 'Lincoln Burrows'
}

hisFamily.brother = ''
// → ❌ Cannot assign to 'brother' because it is a read-only property.

On the other hand, assigning to the variable itself is allowed.

let hisFamily: { readonly brother: string } = {
  brother: 'Lincoln Burrows'
}

hisFamily = {
  brother: ''
}
// → ⭕️

Conclusion

const makes the variable itself non-assignable, while readonly makes the property non-assignable.

By combining const and readonly, you can create an object where both the variable itself and the object's properties are immutable.

const hisFamily: { readonly brother: string } = {
  brother: 'Lincoln Burrows'
}

hisFamily.brother = ''
// ❌ Cannot assign to 'brother' because it is a read-only property.

hisFamily = {
  brother: ''
}
// ❌ Cannot assign to 'hisFamily' because it is a constant.

Happy Coding☀️


This content originally appeared on DEV Community and was authored by Noah


Print Share Comment Cite Upload Translate Updates
APA

Noah | Sciencx (2024-08-22T02:44:44+00:00) The difference between readonly and const in Type Script. Retrieved from https://www.scien.cx/2024/08/22/the-difference-between-readonly-and-const-in-type-script/

MLA
" » The difference between readonly and const in Type Script." Noah | Sciencx - Thursday August 22, 2024, https://www.scien.cx/2024/08/22/the-difference-between-readonly-and-const-in-type-script/
HARVARD
Noah | Sciencx Thursday August 22, 2024 » The difference between readonly and const in Type Script., viewed ,<https://www.scien.cx/2024/08/22/the-difference-between-readonly-and-const-in-type-script/>
VANCOUVER
Noah | Sciencx - » The difference between readonly and const in Type Script. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/22/the-difference-between-readonly-and-const-in-type-script/
CHICAGO
" » The difference between readonly and const in Type Script." Noah | Sciencx - Accessed . https://www.scien.cx/2024/08/22/the-difference-between-readonly-and-const-in-type-script/
IEEE
" » The difference between readonly and const in Type Script." Noah | Sciencx [Online]. Available: https://www.scien.cx/2024/08/22/the-difference-between-readonly-and-const-in-type-script/. [Accessed: ]
rf:citation
» The difference between readonly and const in Type Script | Noah | Sciencx | https://www.scien.cx/2024/08/22/the-difference-between-readonly-and-const-in-type-script/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.