Variables in JavaScript
Photo by Ilija Boshkov on Unsplash

Variables in JavaScript

So far I've written what I've learned about JavaScript, but I haven't really specified what variables and their declaration are. Let's take a more detailed look and remember what these variables are. It's a simple thing when it comes to variables, but repeating them again never hurts, on the contrary, we learn them better!

In JavaScript, variables are used to store values that can be accessed and modified throughout the code. They are an essential part of programming, as they allow us to store and manipulate data in a dynamic way.

There are three known ways of declaring variables in JS:

  • Declaring variables with var

The var keyword is primarily used to declare variables in JavaScript. It has existed since the beginning of the language and is still widely used today.

Here is an example of how to declare a variable with var and assign it a value at the same time:

var myVariable = "Hello!";        

It is important to note that variables declared with var are function-scoped, meaning that they are accessible within the function in which they are declared, as well as any nested functions.

  • Declaring variables with let

The let keyword was introduced in ECMAScript 6 (also known as ES6) as an alternative to var. Like var, it is used to declare variables, but there are a few key differences between the two.

One of the main differences is that variables declared with let are block-scoped, rather than function-scoped like var. This means that they are only accessible within the block of code in which they are declared, as well as any nested blocks.

We can assign a value to a variable when it's declared by using the assignment operator (=).Example of how to declare a variable with let and assign it a value at the same time:

let myVariable;
let myVariable = "Hello there!";        

  • Declaring variables with const

The const keyword was also introduced in ES6 as a way to declare variables that cannot be reassigned. This means that once a value is assigned to a const variable, it cannot be changed. The const keyword is used to declare a variable that cannot be reassigned. This is useful for variables that you want to be constant throughout your code, such as configuration values or constants.

const variables must be assigned a value at the time of declaration, and attempting to reassign a value to a const variable will result in a runtime error.

Example of how to declare a const variable:

const myVar = "Hello, World!";        

It is important to note that the value of a const variable can still be modified, as long as it is not a primitive data type (e.g. a number, string, or boolean). For example, the following is allowed:

const myArray = [];
myArray.push("hello");   // myArray is now ["hello"];        

Tips for choosing which keyword to use

When declaring variables in JavaScript, it is important to choose the right keyword for the task. By using these keywords appropriately, you can better control the scope and mutability of your variables, leading to more maintainable and easier-to-debug code.

Avoid using global variables! Global variables are variables that are accessible from anywhere in your code. While it is possible to use global variables in JavaScript, it is generally considered a bad practice as it can lead to unintended consequences and make your code harder to debug and maintain. Instead, try to use variables with the appropriate scope and limit their accessibility as much as possible.

Here are some tips to help you decide:

  • Use var if you need to declare a variable that will be accessible within a function or globally.
  • Use let if you need to declare a variable that will be accessible within a block of code or within a loop.
  • Use the const keyword when possible. This is a good way to ensure that the value of a variable does not change accidentally. However, it's important to note that the value of a const variable can still be modified if it is a mutable data type such as an object or an array!

Aqib Zahid

Frontend Developer | React Js | JavaScript | Next Js | Typescript | Web Developer

6mo

js

Like
Reply

To view or add a comment, sign in

More articles by Cristian Micicoi

  • First steps towards React

    Once you have a good understanding of JavaScript, you can start learning the basics of React. React is built on top of…

  • JavaScript Conditionals

    JavaScript conditionals are a crucial element of programming, allowing developers to control the flow of their code…

  • JavaScript alert()

    The browsers we use can invoke a system dialog to display information to the user. The system dialog is not related to…

  • JavaScript Array map() method

    Sometimes you may need to take an array and apply some procedure to its elements so that you get a new array with…

  • JavaScript Objects

    In the previous article I presented the basics of what I learned about arrays in JavaScript and about the fact that…

  • JavasScript Arrays

    In JavaScript, objects allow you to store keyed collections of values. That’s fine.

  • JavaScript Functions

    Last weeks I've worked mostly with arrays and functions, and I want to share some of what I've learned about functions.…

  • JavaScript - HTML DOM Methods

    DOM stands for Document Object Model. It is an application programming interface that allows us to create, add, modify…

  • JavaScript Classes

    A class is a blueprint for the object. You can create an object from the class.

  • JavaScript setInterval() method

    Javascript setInterval() Today I learned about another JavaScript method, called setInterval(). I used it in creating…

    2 Comments

Insights from the community

Others also viewed

Explore topics