Mastering Variables in JavaScript: A Beginner's Guide

Mastering Variables in JavaScript: A Beginner's Guide

Understanding the basics of declaring, assigning, and using variables in your JavaScript code

What is a Variable?

A variable in JavaScript in simple language is a container that stores a value, just like a box used to hold something. It may be a number, String, Boolean, etc. And a variable once declared and initialized can be accessed and used at any point of the program.

How to Declare a Variable?

To use a variable we first need to declare it, different keywords are used to create a variable that is let, const, and var. let and const were introduced in ES6 and are currently employed and var is an old way to declare a variable.

var firstName;
let Age;

So, here we have declared two variables, but we didn't assign a value to them yet. So it will return as Undefined it is also a datatype that has no value.

So, now we need to initialize a value to it. To initialize a value we use the assignment operator which is denoted by the (=) sign. The variables can

 firstName = "Bhaskar";
 Age = 24;

Here in this example, we have declared the variable first and then initialized it but we can initialize it at the time of declaration. Just like this one.

let fullName = "Bhaskar Sahu";

let's discuss const, const is used for the constant which means we can not reinitialize a value to it, we can only initialize the value at the time we declare it. So const is used for those values that are not to be changed in the future. let's see an example.

const myBirthYear = 1998;
myBirthyear = 2000;   // This can't be done in const. It will give an error.

There are some rules for variable names that should be followed to name a variable. That are:

  • Variable names should start with alphabets (A to Z or a to z).

  • Numbers can also be used in a variable name. Don't use numbers at the start this will cause an error.

  • No other symbols are used besides the underscore (_) and dollar sign ($).

  • Variable names are Case sensitive.

Difference Between let and var?

The Major difference between let, const, and var is that let and const are block scope and var is function scope. Let's discuss it in detail.

In javascript, the whole document is called global scope. Another is the Local Scope, variables declared inside the functions are considered to be of the local scope and it is further divided into function scope and block scope.

Function Scope

When a variable is declared inside a function, it is only accessible within that function and cannot be used outside that function is called function scope.

Block Scope

A variable declared inside {} curly brackets are block scope. inside the if or switch conditions or inside for or while loops, are accessible within that particular condition or loop. This is a simple example:

if (1 !== 2) {
  var myNumber = 3;    // variable declared using var.
  console.log(myNumber);  
}
console.log(myNumber);  // It will reture 3.

if (1 === 1) {
  let myNum = 1; // variable declared using let inside a block.
  console.log(myNum);
}
console.log(myNum); // It will will an error: myNum is not defined

here in this particular example, we can access the variable declared with var outside the if block. and we can't access the variable declared with let outside the if block. And if you nest the whole block of code inside a function and try to access the variable myNumber you will get an error. Try it yourself.

This is my first article on the JavaScript series more articles are in progress.

Thanks for reading my article on variables in JavaScript, If you loved it, please subscribe to me for more such articles on JavaScript, and also please share it.

Did you find this article valuable?

Support B23R Blog's by becoming a sponsor. Any amount is appreciated!