DEV Community

Cover image for Variables and Datatypes in JavaScript
Ezhil Arasan
Ezhil Arasan

Posted on

Variables and Datatypes in JavaScript

Variables and Data Types in JavaScript are fundamental concepts used to store and manage data in a program. They define how information is declared, stored, and manipulated during execution.
Variables: Declared using var, let, and const to store data values.
Primitive Data Types: Includes Number, String, Boolean, Null, Undefined, BigInt, and Symbol.
Non-Primitive Data Types: Includes Object, Array, and Function used to store complex data.

Variables

A variable is like a container that holds data that can be reused or updated later in the program. In JavaScript, variables are declared using the keywords var, let, or const.

1. var Keyword

The var keyword is used to declare a variable. It has a function-scoped or globally-scoped behaviour.

var n = 5;
console.log(n);

var n = 20; // reassigning is allowed
console.log(n);

Enter fullscreen mode Exit fullscreen mode

2. let Keyword

The let keyword is introduced in ES6, has block scope and cannot be re-declared in the same scope.


let  n= 10;
n = 20; // Value can be updated
// let n = 15; //can not redeclare
console.log(n)
Enter fullscreen mode Exit fullscreen mode

3. const Keyword

The const keyword declares variables that cannot be reassigned. It's block-scoped as well.

const n = 100;
// n = 200; This will throw an error
console.log(n)
Enter fullscreen mode Exit fullscreen mode

The Variable Name

There are certain rules that apply when naming a variable. Some rules are programming-language specific, others apply to all programming languages:
A variable name cannot contain spaces.
A variable name cannot start with a number.
A variable name cannot be a reserved word like if, else, for, function etc.

Difference between static and non-static variables in Java
In Java, variables are mainly categorized into two main types based on their working and memory allocation, and these two variables are static variables and non-static variables. The main difference between them is listed below:
Static variables: These are variables that are shared among all the instances of a class.
Non-static variables: These are variables that belong to each individual instance of a class.

Java Static Variables

When a variable is declared as static, then a single copy of the variable is created and shared among all objects at a class level. Static variables are essentially, global variables.

// Java program to demonstrate execution
// of static blocks and variables

class Geeks {
    // static variable
    static int a = m1();

    // static block
    static { System.out.println("Inside static block"); }

    // static method
    static int m1()
    {
        System.out.println("from m1");
        return 20;
    }

    public static void main(String[] args)
    {
        System.out.println("Value of a : " + a);
        System.out.println("from main");
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

from m1
Inside static block
Value of a : 20
from main

Java Non-static variables

Non-static variables are variables that belongs to a specified object of a class, it is also known as instance variable. These variables are declared outside of a method, constructor or block. Each object created from the class gets its own separate copy of these variables.

// Java program to demonstrates 
// the working of non-static variables

public class Geeks 
{  
    // declaration of non-static variables.
    public String name; 
    String division;
    private int age;    

    // Constructor that initialize non-static variable.
    public Geeks(String sname)  
    {  
        name = sname;  
    }  

    //Method to initialize non-static variable. 
    public void setDiv(String sdiv)  
    {  
        division = sdiv;  
    }  

    public void setAge(int sage)  
    {  
        age = sage;  
    }  

    // Method to display the values
    public void printstud()  
    {  
        System.out.println("Student Name: " + name );  
        System.out.println("Student Division: " + division);   
        System.out.println("Student Age: " + age);  
    }  

    public static void main(String args[])  
    {  
        Geeks g = new Geeks("Monica");  
        g.setAge(14);  
        g.setDiv("B");  
        g.printstud();  
    }  
}
Enter fullscreen mode Exit fullscreen mode

Output

Student Name: Monica
Student Division: B
Student Age: 14

Data Types

JavaScript supports various datatypes, which can be broadly categorized into primitive and non-primitive types.
Primitive Datatypes

Primitive datatypes represent single values and are immutable.

1. Number: Represents numeric values (integers and decimals).

let n = 42;
let pi = 3.14;

Enter fullscreen mode Exit fullscreen mode

2. String: Represents text enclosed in single or double quotes.

let s = "Hello, World!";
Enter fullscreen mode Exit fullscreen mode

3. Boolean: Represents a logical value (true or false).

let bool= true;
Enter fullscreen mode Exit fullscreen mode

4. Undefined: A variable that has been declared but not assigned a value.

let notAssigned;
console.log(notAssigned);
Enter fullscreen mode Exit fullscreen mode

5. Null: Represents an intentional absence of any value.


let empty = null;
Enter fullscreen mode Exit fullscreen mode

6. Symbol: Represents unique and immutable values, often used as object keys.

let sym = Symbol('unique');
Enter fullscreen mode Exit fullscreen mode

7. BigInt: Represents integers larger than Number.MAX_SAFE_INTEGER.

let bigNumber = 123456789012345678901234567890n;

Enter fullscreen mode Exit fullscreen mode

**
Non-Primitive Datatypes**

Non-primitive types are objects and can store collections of data or more complex entities.

1. Object: Represents key-value pairs.


let obj = {
    name: "Amit",
    age: 25
};
Enter fullscreen mode Exit fullscreen mode

2. Array: Represents an ordered list of values.


let a = ["red", "green", "blue"];
Enter fullscreen mode Exit fullscreen mode

3. Function: Represents reusable blocks of code.


function fun() {
    console.log("GeeksforGeeks");
}
Enter fullscreen mode Exit fullscreen mode

Bits and Bytes in Programming
Bits and bytes are the smallest units of data in a computer.
A bit is a single binary digit, with a value of either 0 or 1.
A byte is a group of 8 bits.

What is a Bit?

A bit is the smallest possible unit of data in a computer.
One bit holds a value of either 0 or 1.
Bits are stored in different ways:

In computer memory, a bit is stored as electrical voltage, where a voltage above a certain threshold represents a 1, and a voltage below that threshold represents a 0.
In hard disk drives, a bit is stored as magnetism, where an area magnetized in one orientation represents a 1, and a magnetized area in the opposite orientation represents a 0.
In CDs, DVDs, and Blu-ray discs, a bit is stored as either a pit, or a flat area. A pit is an area where the surface is lower than the surrounding surface, and that represents a 1. A flat area is when there is no pit, and that represents a 0.
Enter fullscreen mode Exit fullscreen mode

What is a Byte?

A byte is a group of 8 bits, like 10001011 for example.

Each bit can be either 0 or 1, and with 8 bits in a byte, there are 28 = 256 different values a byte can have.

But simply storing just one bit is not very useful. We need to store more bits together to represent larger amounts of data.

Storing Groups of Bytes

Like we have seen, it is possible to use a single byte to store a single character, a number, or a color.

But normally, modern computers use more than one byte to store something.

Data Storage Units

When storing data, we can use different units to measure the size of the data.

In data measurement units, the capital letter "B" is used to represent "byte", and the lower case letter "b" is used to represent "bit".

Storing many bytes, we use units:

Bytes (B)
Kilobytes (kB)
Megabytes (MB)
Gigabytes (GB)
Terabytes (TB)
Enter fullscreen mode Exit fullscreen mode

The International System of Units (SI) defines the prefixes:

kilo- (k), meaning 1 000
mega- (M), meaning 1 000 000
giga- (G), meaning 1 000 000 000
tera- (T), meaning 1 000 000 000 000
Enter fullscreen mode Exit fullscreen mode

Refrence
https://www.geeksforgeeks.org/javascript/javascript-tutorial/
https://www.w3schools.com/programming/prog_binary_numbers.php
https://www.w3schools.com/programming/prog_variables.php
https://www.geeksforgeeks.org/javascript/variables-datatypes-javascript/
https://www.geeksforgeeks.org/java/difference-between-static-and-non-static-variables-in-java/

Top comments (0)