Mastering the Building Blocks of JavaScript: Understanding Var, Let and Const.
Unlocking Your JavaScript Power!
Introduction
Hello, and welcome to my article. If you're new to programming or trying to improve your understanding of these topics, you've come to the right place. In this article, I'll break down these concepts in a simple and easy-to-understand way, so you can feel confident using them in your code. We'll cover what each of these terms means, why they're important, and how you can use them in your programs. So, let's jump right in!
Variables
Variables are containers for storing data value(s).
First up, let's talk scopes. Scopes define where in your code a variable is accessible. So, when you're working with your variables, you'll want to make sure you're using the right scope. But don't worry, it's not as scary as it sounds. Let's explore together
Block Scope
Is a concept in programming where variables declared within a specified block of code are accessible within that block or any nested block within it.
Programming languages that support block scope include Js and C++.
Block scope helps prevent naming conflicts between different parts of a program. Well, let's dive into coding example😉
for (let i = 0; i < 10; i++) {
let message = `Current value of i is ${i}`;
console.log(message);
}
// We cannot access the message variable outside of the for loop
Global Scope
Variables declared outside of a function meaning they can be accessed anywhere in the code, including inside the function.
However, it is generally recommended to avoid using global variables to prevent naming conflicts and unexpected value changes.
let carName = "Volvo";
// code here can use carName
function myFunction() {
// code here can also use carName
}
Var
Let's have an example we're working on a project where we have multiple functions with nested scopes, and we want to declare a variable that can be accessed and modified by all the functions in that specific scope.
In this scenario, we can use the var
keyword to declare a variable at the top-level scope of your program, which will be accessible by all the functions in that scope.
function outerFunction() {
var myVariable = "Hello Subscriber!";
function innerFunction() {
console.log(myVariable); // Output: Hello Subscriber!
myVariable = "Changed variable value";
}
innerFunction();
console.log(myVariable); // Output: Changed variable value
}
outerFunction();
We learn that var is globally scoped- function scope and can be reassigned.
It's not recommended as it can cause conflicts.
Let
Let is used to declare a block-scoped variable. It provides an alternative to the var
keyword.
function myFunction() {
let x = 10; // x is declared with let and is only accessible within this function
if (true) {
let y = 20; // y is a different variable than x and is only accessible within this block
console.log(x); // x is accessible within this block
console.log(y); // y is accessible within this block
}
console.log(x); // x is also accessible here
console.log(y); // y is not accessible here
}
We can see the output above. Hurray!
Const
Const is block-scoped, it cannot be reassigned once it has been initially set, or even redeclared.
const number = 10; // declaring variable
console.log(number); // output would be 10
number = 5; // TypeError: Assignment to constant variable.
const variables are often used to declare values that should not be changed throughout the program, such as mathematical constants.
Recap
In conclusion, variables are essential components of programming languages that store data values that can be manipulated or modified within a code block. Understanding the concept of variables is crucial for any beginner looking to dive into programming.
In our next article, we will be discussing the different data types in programming, along with functions and Methods. These topics will help you develop a deeper understanding of programming language and prepare you for more complex coding tasks. Stay tuned!
Network
Are you a fan of all things data? I've recently published some articles on Medium about data and I'm wondering if you'd be interested in checking them out sometime?
Medium - CorneliusLochipi
Please remember to clap enthusiastically to help this message reach a wider audience. Thank you! Tadaa!