JavaScript Basic : The Language of the Web

JavaScript Basic : The Language of the Web

Mastering Variables, Data Types, Operators, and Control Flow: The Building Blocks of JavaScript

Introduction

JavaScript a programming language, that was created to make the web page alive.

The programms written in JavaScript are called “Scripts”. It runs in the browser and works alongside HTML and CSS to create dynamic, user-friendly websites.

JavaScript was created by Brendan Eich in 1995. He was a developer at Netscape Communications Corporation.

Headshot of Brendan Eich

💡
JavaScript and Java are not related. they share the name because JavaScript was initially marketed as "Java’s little brother" during its release to ride on Java's popularity in the mid-90s. The similarity in names is purely branding and has no technical connection.

Understanding JavaScript

What are Variables ?

Variables are containers in JavaScripts that are used to store different types of data.

You can declare variables using the var, let, or const keywords:

Declaration of Variables

  • var: Function-scoped or globally scoped. Can be re-declared and updated.

  • let: Block-scoped. Can be updated but not re-declared in the same scope.

  • const: Block-scoped. Cannot be updated or re-declared.

Example :

let box;

Now, we add some data inside the box.

box = 'Apples'; // store a string "Apple" in the box container

  1. Only use var if you MUST support old browsers.

  2. A variable should be declared only once. A repeated declaration of the same variable is an error. ⚠️

Variable Naming Rules

  • Must begin with a letter, $, or _.

  • Case-sensitive.

  • Cannot use reserved keywords.


Data Types in JavaScript

JavaScript supports two types of Data Types :- Primitive Data Types and Non-Primitive Data types.

Primitive Data Types

Primitive data types represent a single value. They are immutable, meaning their values cannot be altered once created.

  1. String

  • Represents textual data.

  • Enclosed in single ('), double (") or backticks (` ).

  • Examples:

      let name = "John";
      let greeting = 'Hello';
      let message = `Hi, ${name}`;
    
  1. Number

  • Represents both integer and floating-point numbers.

  • Includes special values like Infinity, -Infinity, and NaN (Not-a-Number).

  • Examples:

      let age = 25;
      let price = 99.99;
      let result = 10 / 0; // Infinity
      let invalid = "hello" * 2; // NaN
    
  1. BigInt

  • Used to represent integers larger than the Number type can hold.

  • Created by appending n to an integer.

  • Examples:

      let bigNumber = 123456789012345678901234567890n;
    
  1. Boolean

  • Represents a logical entity and has two possible values: true or false.

  • Examples:

       let isActive = true;
      let isAdult = false;
    
  1. Undefined

  • A variable declared but not assigned any value is undefined.

  • Examples:

      let x;
      console.log(x); // undefined
    
  1. Null

  • Represents an intentional absence of a value or an empty value.

  • Examples:

      let empty = null;
    
  1. Symbol

  • Introduced in ES6, used to create unique identifiers.

  • Useful for creating unique object properties.

  • Examples:

      let uniqueID = Symbol('id');
    

Non-Primitive Data Types

Non-primitive data types are objects and their subtypes. They are mutable, meaning their values can be altered.

  1. Object

  • Represents a collection of key-value pairs.

  • Examples:

      let person = {
        name: "Kumar",
        age: 20,
        isStudent: false,
      };
    
  1. Array

  • A special type of object used to store multiple values.

Examples:

let colors = ["red", "green", "blue"];

By using typeof operator we could easily find, to which data type the variable belongs.


Operators in Javascript

CategoryOperatorDescription
Arithmetic+, -, *, /, %, **Perform basic arithmetic operations (addition, subtraction, multiplication, division, modulus, exponentiation).
Assignment=, +=, -=, *=, /=, %=Assign and update values of variables.
Comparison==, !=, ===, !==, <, >, <=, >=Compare two values. == compares values, === compares values and types.
Logical&&, `
Bitwise&, `, ^ , ~ , << ,>> , >>>`
Unary+, -, ++, --, typeof, delete, !Perform operations on a single operand (e.g., increment, decrement, check type, delete property, negate).
Ternary? :Acts as a shorthand for if...else.
String+Concatenates strings.
Typetypeof, instanceoftypeof checks data type. instanceof checks if an object is an instance of a specific class.
Spread/Rest...Expands elements of an array or collects arguments into an array.
Destructuring{} / []Extract values from objects or arrays.
Nullish Coalescing??Returns the right-hand value if the left-hand value is null or undefined.
Optional Chaining?.Access object properties without throwing errors for null or undefined.
Comma,Evaluates multiple expressions and returns the result of the last expression.

Control Flow in JavaScript

Control flow statements determine the execution order of instructions in a program execute.

Conditional Statements

The Conditional Statements in JavaScript are :-

  1. if Statement

Executes a block of code if a condition is true.

if (x > 0) {
  console.log("x is positive");
}

  1. if-else Statement

Executes one block of code if a condition is true, and another if it is false.

if (x > 0) {
  console.log("x is positive");
} else {
  console.log("x is not positive");
}

  1. else if Ladder

Allows multiple conditions to be checked.

if (x > 0) {
  console.log("x is positive");
} else if (x < 0) {
  console.log("x is negative");
} else {
  console.log("x is zero");
}

  1. switch Statement

Used to perform different actions based on different conditions.

switch (fruit) {
  case "Apple":
    console.log("Apple selected");
    break;
  case "Banana":
    console.log("Banana selected");
    break;
  default:
    console.log("No fruit selected");
}


Conclusion

In conclusion, from this article, we got to know the importance of var, let, and const in managing variable scope effectively. We also learned how conditional statements help in handling complex logic and making dynamic decisions. Mastering these basics forms the foundation for building clean, responsive, and maintainable JavaScript applications.

If you like this article subscribe to hashnode/Kumar-nirupam ——>

Want More—- ?

Follow —> Kumar Nirupam , Twitter/x