Const, var, let   us know the difference?!

Const, var, let us know the difference?!

Keep Your Friendship OK with JavaScript !

JavaScript Is Awful!

JavaScript is a programming language that is commonly used to create interactive effects within web browsers. It is a client-side scripting language, which means that it is executed on the user's computer rather than on a server. JavaScript is often used to :

  • add interactivity to web pages, such as responding to user input, creating drop-down menus, and displaying notifications.

  • It is also used to build mobile applications and create server-side applications.

And, According to the website W3Techs, as of January 2021, JavaScript was used by 95.9% of all websites with a known technology. This suggests that a very large majority of websites use JavaScript in some way And hence JS is NOT AWFUL AT ALL !

Let's declare some variables :

Well, our three cute elements let, var, and const are used to declare variables,

let's know the difference :

const is used to declare a constant variable, which means that the value of the variable cannot be reassigned.

const myName = "Mustapha";

var is used to declare a variable. It is used less frequently than let because it has some limitations, such as the fact that variables declared with var are hoisted to the top of the enclosing function or global scope, which can lead to some unexpected behavior.

var myName = "Mustapha";

and my favorite to declare variable is

let is similar to var, but it is used to declare variables that are block-scoped, rather than function-scoped like var. This means that a variable declared with let is only accessible within the block of code in which it is defined, such as a for loop or an if statement.

let myName = "Mustapha";

In general, it is recommended to use const whenever possible and to use let if you need to reassign a value to a variable. It is generally best to avoid using var unless you have a specific reason to do so

Variables block-scoped vs Variables function-scoped :

--> Block-scoped variables are variables that are only accessible within the block of code in which they are defined. This means that they are only visible within the curly braces { } that define the block of code.

let's see an exemple :

for (let i = 0; i < 10; i++) {
  console.log(i);  // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}
console.log(i);  // ReferenceError: i is not defined

In this example, the variable i is only accessible within the for loop and is not defined outside of it.

--> Function-scoped variables are variables that are only accessible within the function in which they are defined. This means that they are only visible within the curly braces { } that define the function.

function myFunction() {
  var message = "Hello, world!";
  console.log(message);  // "Hello, world!"
}
console.log(message);  // ReferenceError: message is not defined

In this example, the variable message is only accessible within the myFunction function and is not defined outside of it.

It is generally recommended to use block-scoped variables whenever possible, as they can help to prevent accidental reassignments and can make it easier to understand the scope of a variable. Function-scoped variables can sometimes lead to confusion, as they can be accessed from anywhere within the function in which they are defined, even if they are defined within a block of code.