strict mode 🤫 And Some Basic Differences Between let and var Keyword

·

2 min read

strict mode 🤫 And Some Basic Differences Between let and var Keyword

The scoping behavior is one of the major differences between the 'let' and 'var' keywords. But we will discuss this difference later on.

In this lesson, we will talk about some basic differences that we should know. So let's start 🤘

⭐With "var", you can redeclare a variable multiple times without getting an error. This behavior can introduce bugs in your code.

var age = 8;
var age = 9;

This won't throw an error, both declarations are allowed. However, the variable will hold the latest assigned value (age will be 9 in this case).

⭐With "let", redeclaring a variable is not allowed and will result in a syntax error.

let age = 8;
let age = 9; // This will throw a syntax error

strict mode

Using strict mode is considered a good practice in modern JavaScript development. When you use strict mode, the JavaScript engine enforces a stricter set of rules and checks on your code, catching common mistakes and reducing the silent errors that could occur in non-strict mode.

for example:- Without strict mode

myVar = 10; // Undeclared variable
console.log(myVar); // 10

In strict mode, attempting to assign a value to an undeclared variable will throw an error. There are many more uses of strict mode that we will see as we move forward with our learning.

To enable strict mode, you include the following string 'use strict' at the beginning of a script.

'use strict';

In the next lesson, we will discuss about const keyword, different datatypes , typeof operator.

I’m on @buymeacoffee If you like my work, you can buy me a coffee and share your thoughts https://www.buymeacoffee.com/yashika227x