Errors in JavaScript
Hey everyone, I am going to talk about three errors in JavaScript that can occur due to let and const. The errors are ReferenceError, SyntaxError and TypeError.
ReferenceError — ReferenceError occurs when a program is executed and code could not find the reference to any variable.
Now look at this example,
When we call a ‘var’ type of variable it shows ‘undefined’ whereas for ‘let and const’ type for variable it gives ReferenceError. In javaScript memory is always allocated before initialization of variables. But still this error occurs because whenever a variable is declared in the global scope with the keyword ‘var’ it is attached to the global or window object whereas for ‘let’ and ‘const’ type of variable memory is allocated to its variable but in a separate memory location called ‘Script’. Thus ‘let’ and ‘const’ variables should always be called only after declaring it otherwise it behaves as if the variable is not present. The zone between the calling of the ‘let and const’ variable and it’s declaration is called Temporal dead zone. In this zone only reference error occurs.
SyntaxError — This type of error occurs when a ‘let’ or ‘const’ variable is being declared twice or when a const variable is not being initialized at the time of declaration. In syntax error the code is not accepted, it does not run at all.
Get Nisha Sen’s stories in your inbox
Join Medium for free to get updates from this writer.
Here are all the examples to help you understand properly,
In the case of ‘var’ type it is very lenient. It can be declared twice and can be initialized a multiple number of times.
In ‘let’ you can declare a variable at one place and can initialize it later whereas ‘const’ is more strict. It should be declared and initialized at one place.
TypeError — This type of error occurs when a ‘const’ variable is initialized again. ‘Const’ is a very strict type of variable.
To avoid these errors, it is suggested to always initialize all the variables at the top of the code and then to proceed with the logic part.
Try to use the ‘const’ variable always if you see a variable never changes, else use the ‘let’ variable as these variables are more strict and it is good practice to use them other than ‘var’.
Hope you enjoyed learning this. If you find this blog helpful do like, share and comment. This will motivate me in my journey of blogging. Any feedback and suggestions are always welcome. Thank you and happy learning 🎉.





