Const with Undefined


#1

https://www.codecademy.com/courses/learn-javascript-introduction/lessons/variables/exercises/unset-variable?action=lesson_resume&course_redirect=introduction-to-javascript

When I try to create a variable with const then after logging the variable there is an error message, while, undefined is not printed. Why does it work with let and not const?


const notDefined;
console.log(notDefined);


#2

constant variable can’t be changed, for example:

const example = 'hello world'
example = 'this will give an error'

this gives an error, given we can’t update/re-assign the variable. So being able to declare an empty constant variable, would be pointless, we can’t change it, which is why it gives an error

let variable can be changed/updated/re-assigned.


#3

Thanks a lot for this helpful reply.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.