Primitive Datatypes

  1. Number
    • Represents both integer and floating-point numbers.
    • Example:
let integer = 42; let float = 3.14;
- Special values: `Infinity`, `-Infinity`, `NaN` (Not-a-Number).
	
	
  1. String
    • Represents sequences of characters.
    • Can be defined using single quotes ('), double quotes ("), or backticks (`) for template literals.
    • Example:
let singleQuote = 'Hello';
let doubleQuote = "World";
let templateLiteral = `Hello, ${doubleQuote}!`;
  1. Boolean
    • Represents logical values: true and false.
    • Example:
let isTrue = true; let isFalse = false;
  1. Undefined

    • Represents a variable that has been declared but not assigned a value.

    • Example:

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

  2. Null

    • Represents the intentional absence of any object value.

    • Example:

      let y = null;

  3. Symbol (ES6)

    • Represents a unique and immutable value often used as object keys.

    • Example:

      let sym = Symbol('description');

  4. BigInt (ES11)

    • Represents whole numbers larger than the maximum safe integer (Number.MAX_SAFE_INTEGER).

    • Example:

      let bigInt = 1234567890123456789012345678901234567890n;

Composite (Complex) Datatypes

  1. Object

    • A collection of properties, where each property is a key-value pair.

    • Keys can be strings or symbols, and values can be any datatype.

    • If Keys are number data type then value is implicitly converted into strings.

    • Example:

      let obj = { name: 'Alice', age: 30, greet: function() { console.log('Hello!'); } };

  2. Array

    • A list-like collection of elements, accessible via numerical indices.

    • Example:

      let arr = [1, 2, 3, 'four', { five: 5 }];

Special Datatypes

  1. Functions
    • A callable object that encapsulates code to be executed.

    • Functions can be defined in various ways (function declarations, expressions, arrow functions).

    • Example:

      function greet() { return 'Hello!'; } let greetArrow = () => 'Hello!';

Type Conversion and Coercion

  • Implicit Type Coercion: JavaScript automatically converts values from one datatype to another in certain operations.

    • Example:

      console.log('5' + 5); // '55' (string concatenation) console.log('5' - 2); // 3 (numeric subtraction)

  • Explicit Type Conversion: Manually converting from one datatype to another using functions.

    • Example:

      let num = Number('123'); // Converts string to number let str = String(123); // Converts number to string