DEV Community

Cover image for JavaScript Struggles - Part 1 | Defending Variables
Abdelrahman Dwedar
Abdelrahman Dwedar

Posted on • Edited on

2 2

JavaScript Struggles - Part 1 | Defending Variables

Defending variables in JS have its own way.

We have three ways to defend a variable let, var, const.

Var Let Const
Changeable
Block Scope
Global Scope
Make Arrays

We mostly use let because of the block scope which I'll explain in the below. 👇🏻


Let

The keyword let makes a variable only useable within the scope it made in, you can't use it outside that scope.

E.g.

{
let num = 10;
console.log(num); // Outputs: 10
}
console.log(num); // ERROR
view raw let-keyword.js hosted with ❤ by GitHub

Var

The keyword var makes a global variable, you can use it everywhere in the code.

E.g.

{
var num = 10;
console.log(num); // Outputs: 10
}
console.log(num); // Outputs: 10
view raw var-keyword.js hosted with ❤ by GitHub

Const

The keyword const makes an unchangeable variable, you can't change its value.

E.g.

const pi = 3.14159265359;
pi = 4; // ERROR

Top comments (1)

Collapse
 
abdelrahman_dwedar profile image
Abdelrahman Dwedar

Thaks for sharing that with me, sorry for the mistake. 🙏🏻
I've heard that the block scope is taking less memory space then the general. Thanks for clearifying. 👍🏻👍🏻

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay