Scalable Business for Startups

Get the oars in the water and start rowing. Execution is the single biggest factor in achievement so the faster and better your execution.

+91 9472774549 thebytemind@gmail.com Looking for collaboration for your next creative project?

Chapter 3: Variables, Data Types, and Operators in JavaScript

1. Variables

In JavaScript, variables are used to store data values. You can create variables using three keywords: var, let, and const.

  • var: The traditional way to declare variables. It has function scope and can be re-assigned.
    var name = "Alice"; 
    var age = 30;
  • let: Introduced in ES6, let allows you to create block-scoped variables, meaning they are only accessible within the block they are defined in.
    let score = 100;
    score = 150; // This is allowed
  • const: Also introduced in ES6, const is used to declare constants, meaning the variable cannot be re-assigned. It is block-scoped as well.
    const pi = 3.14;
    // pi = 3.14159; // This will throw an error
2. Data Types

JavaScript has several built-in data types, which can be categorized into two groups: Primitive types and Reference types.

Primitive Types

These are the most basic data types in JavaScript:

  • String: Represents text.
    let greeting = "Hello, world!";
  • Number: Represents both integer and floating-point numbers.
    let count = 42;
    let price = 19.99;
  • Boolean: Represents true or false.
    let isActive = true;
  • Undefined: A variable that has been declared but not assigned a value.
    let user;
    console.log(user); // undefined
  • Null: Represents an intentional absence of any object value.
    let emptyValue = null;
  • Symbol: A unique and immutable primitive value, often used as keys for object properties (ES6).
    const uniqueID = Symbol('id');
  • BigInt: A numeric type that can represent integers with arbitrary precision (ES11).
    const bigNumber = BigInt(123456789012345678901234567890);

Reference Types

These include more complex data structures:

  • Object: A collection of key-value pairs.
    let person = {
        name: "Alice",
        age: 30
    };
  • Array: An ordered list of values.
    let colors = ["red", "green", "blue"];
3. Operators

JavaScript provides a variety of operators that can be used to perform operations on variables and values.

Arithmetic Operators

Used to perform mathematical calculations.

Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%), Increment (++), Decrement (--).

let sum = 5 + 3; // 8
let product = 5 * 3; // 15

Assignment Operators

Used to assign values to variables.

Assignment (=), Add and assign (+=), Subtract and assign (-=), etc.

let x = 10;
x += 5; // x = 15

Comparison Operators

Used to compare values.

Equal to (==), Strict equal to (===), Not equal to (!=), Strict not equal to (!==), Greater than (>), Less than (<), etc.

let isEqual = (5 == '5'); // true (type coercion)
let isStrictEqual = (5 === '5'); // false (no type coercion)

Logical Operators

Used to combine multiple conditions.

AND (&&), OR (||), NOT (!).

let result = (true && false); // false
Example Code

Here’s a simple example that combines variables, data types, and operators:

// Variable declaration
let name = "John Doe"; // String
let age = 25; // Number
let isEmployed = true; // Boolean

// Arithmetic operation
let yearsToRetirement = 65 - age;

// Object
let user = {
    name: name,
    age: age,
    isEmployed: isEmployed,
    yearsToRetirement: yearsToRetirement
};

// Output
console.log(user);
Conclusion

Understanding variables, data types, and operators is essential for programming in JavaScript. As you practice, you'll become more comfortable using them to build dynamic applications.