Nadeem's blog

JavaScript Variables

work in progress, last updated: May 14, 2020

Introduction

Variables play an important role in computer programming because they enable programmers to write flexible programs. Rather than entering data directly into a program, a programmer can use variables to represent the data.

Declaring, assigning, and modifying variables

To create a variable in JavaScript, use the let keyword. The example below creates/declares a variable with the name myVariable

        
          let myVariable;
        
      

We can put some data into it by using the assignment operator =

        
          let myVariable = 'Hello Coders!'; // store a string to myVariable
        
      

We can think of a “variable” as a "container" for data, with a uniquely-named label on it. For example, the variable myVariable can be imagined as a container labeled "myVariable" with the value "Hello Coders!" in it. Knowing that, we can put any value in the container. We can also change it as many times as we want!

        
          let myVariable = 'Hello Coders!';
          myVariable = 'Hello Powercoders!'; // value changed
        
      

Types of variables data

Variables can store more than strings. They can store strings, numbers, formulas, booleans, arrays, objects, or a compination of data types. Basicly any type of complex data. Below are some examples

        
          let number = (1 + 1) - (2*2); //numbers & formulas
          let text = "this is a string"; //strings
          let cond = true; //booleans
          let data = [1,2,3,"tring"]; //array
          let object = {
            name: "string",
            age: 23
          };