Variables & DataTypes in JavaScript

·

4 min read

Variables & DataTypes in JavaScript

Variables in JS

Variables are like containers for data, i.e., they store the data.
They get memory location in which they store the data

They can be iniciated using :

  • let : let is used to initiate the variable when we want to change the contents of that variable
    In short, let is used when we want to change the values of that variable

      let a = 20; // The Variable a has the value 10
    

const : const is used for consent variable, it is used to initiate the variable when we want that the variable should not change its content.
In short, const is used to give consent value to a variable

const Name = "Karan"; // The variable 'Name' stores the value "Karan"

Variables can be declared and initiated in JS

  1. Naming Conventions of variable in JS :

    • Variable names must start with a letter, underscore (_), or dollar sign ($).

    • Subsequent characters can also include digits (0-9).

    • Variable names are case-sensitive (myVar and myvar are different).

Reserved Words: Reserved words are the word that are pre reserved, i,e,. they have a specific purpose and cannot be used to name a variable.

Reserved words in JavaScript are like break, case, class, etc.
For more reserved words Click here.

Data Types in JS

Data Types : The type of data(like number, string, etc) hold by the variable is known as its data type.

let a = 20; // Here data type of 'a' is number.
const Name = "Karan"; // Here data type of 'Name' is string.

There are two types of data types in JS :

  1. Primitive Data Types:

    • Number: Represents both integer and floating-point numbers. For example, 42 or 3.14.

    • String: Represents a sequence of characters, used for text. Strings are enclosed in single quotes, double quotes, or backticks. For example, 'Hello', "World", or Hello World.

    • Boolean: Represents a logical entity and can have two values: true or false.

    • Undefined: A variable that has been declared but not assigned a value has the type undefined.

    • Null: Represents the intentional absence of any object value. It is a special keyword denoting a null value.

    • Symbol: Represents a unique and immutable primitive value, often used to identify object properties uniquely.

    • BigInt: Represents whole numbers larger than the largest number JavaScript can reliably represent with the Number type.

        // Primitive Data Types
      
        // Number: Represents both integer and floating-point numbers
        let age = 42; // Example of an integer
        let pi = 3.14; // Example of a floating-point number
      
        // String: Represents a sequence of characters, used for text
        let greeting = 'Hello'; // String using single quotes
        let place = "World"; // String using double quotes
        let message = `Hello World`; // String using backticks (template literals)
      
        // Boolean: Represents a logical entity and can have two values: true or false
        let isJavaScriptFun = true; // Boolean value true
        let isTired = false; // Boolean value false
      
        // Undefined: A variable that has been declared but not assigned a value
        let notAssigned; // This variable is undefined
      
        // Null: Represents the intentional absence of any object value
        let emptyValue = null; // Null value
      
        // Symbol: Represents a unique and immutable primitive value
        let uniqueId = Symbol('id'); // Creating a new Symbol
      
        // BigInt: Represents whole numbers larger than the largest number JavaScript can reliably represent with the Number type
        let bigNumber = BigInt(123456789012345678901234567890); // BigInt value
      
  2. Non-Primitive (Reference) Data Types:

    • Object: A collection of properties, where each property is a key-value pair. Objects can represent more complex entities and can include other objects, arrays, functions, etc.

    • Array: A special type of object used to store ordered collections of values. Arrays are zero-indexed and can hold elements of any data type.

        // Non-Primitive (Reference) Data Types
      
        // Object: A collection of properties, where each property is a key-value pair
        let person = {
          name: 'John', // Property 'name' with value 'John'
          age: 30 // Property 'age' with value 30
        };
      
        // Array: A special type of object used to store ordered collections of values
        let colors = ['red', 'green', 'blue']; // Array of strings
      

To read about functions of array you cay check out my article : Top 10 JS_arrayMethods