5. Values, Types, and Operators#

5.1. Variable declare#

In JavaScript, you can introduce variables using various keywords such as var, let, and const

5.1.1. var#

  • Function Scoped: Variables declared with var are function-scoped. This means that they are accessible throughout the function in which they are declared, regardless of block scope.

  • Hoisting: Variables declared with var are hoisted to the top of their function or global scope. This means you can access the variable before it’s declared without causing an error, although its value will be undefined.

  • Re-assignable: You can reassign values to variables declared with var.

5.1.2. let#

  • Block Scoped: Variables declared with let are block-scoped, meaning they are only accessible within the block they are defined in (e.g., within loops, conditionals, or functions).

  • No Hoisting: Unlike var, variables declared with let are not hoisted to the top of their scope. They are only accessible after they are declared.

  • Re-assignable: Variables declared with let can be reassigned new values.

* Preferred Modern Choice: let is the preferred choice for variable declaration in modern JavaScript due to its block scope and lack of hoisting.

5.1.3. const#

  • Block Scoped: Like let, variables declared with const are block-scoped.

  • No Hoisting: Similar to let, const variables are not hoisted.

  • Immutable: Variables declared with const cannot be reassigned once they are initialized. However, this does not mean the value itself is immutable; if the value is an object or array, its properties or elements can still be modified.

// Declaring a variable using var
var message = "Hello, World!";
console.log(message); // Output: Hello, World!

// Declaring a variable using let
let count = 10;
console.log(count); // Output: 10

// Declaring a constant variable using const
const PI = 3.14;
console.log(PI); // Output: 3.14

5.2. Values#

  • Every value has a type that determines its role

    • Some values are numbers, some values are pieces of text, some values are functions, and so on.

    typeof 42; // Outputs: "number"
    typeof "Hello"; // Outputs: "string"
    typeof true; // Outputs: "boolean"
    typeof undefined; // Outputs: "undefined"
    typeof null; // Outputs: "object" (which is a known quirk)
    typeof {}; // Outputs: "object"
    typeof []; // Outputs: "object" (arrays are also objects)
    typeof function () {}; // Outputs: "function"
    

5.3. Numbers#

  1. In a JavaScript program, they are written as follows : 13

  2. For very big or very small numbers, you may also use scientific notation by adding an e (for exponent), followed by the exponent of the number. 2.998e8

  3. Special numbers: There are three special values in JavaScript that are considered numbers but don’t behave like normal numbers.

    1. The first two are Infinity and -Infinity

    2. Third is NaN, stands for “not a number”

  • JavaScript uses a fixed number of bits, 64 of them, to store a single number value.

  • The important thing is to be aware of it and treat fractional digital numbers as approximations, not as precise values.

5.4. Arithmetic operator#

  1. Addition (+): Adds two operands.

    1. let sum = 3 + 5; // sum will be 8

  2. Subtraction (-): Subtracts the right operand from the left operand.

    1. let difference = 10 - 4; // difference will be 6

  3. Multiplication (*): Multiplies two operands.

    1. let product = 2 * 6; // product will be 12

  4. Division (/): Divides the left operand by the right operand.

    1. let quotient = 8 / 2; // quotient will be 4

  5. Remainder/Modulus (%): Returns the remainder when the left operand is divided by the right operand.

    1. let remainder = 10 % 3; // remainder will be 1

  6. Exponentiation (**): Raises the left operand to the power of the right operand.

    1. let result = 2 ** 3; // result will be 8

5.5. String#

  1. Strings cannot be divided, multiplied, or subtracted, but the + operator can be used on them. It does not add, but it concatenates—it glues two strings together.

    "con" + "cat" + "e" + "nate";
    
  2. Backtick-quoted strings, ${}, usually called template literals. its result will be computed, converted to a string, and included at that position.

    `half of 100 is ${100 / 2}`; // half of 100 is 50
    

5.6. Logical Operators#

|| has the lowest precedence, then comes &&, then the comparison operators (>, ==, and so on), and then the rest.

  1. Logical AND (&&): This operator returns true if both operands are true; otherwise, it returns false.

    let x = true;
    let y = false;
    console.log(x && y); // Outputs: false
    
  2. Logical OR (||): This operator returns true if at least one of the operands is true; otherwise, it returns false.

    let a = true;
    let b = false;
    console.log(a || b); // Outputs: true
    
  3. Logical NOT (!): This operator returns the opposite boolean value of the operand. If the operand is true, it returns false, and if the operand is false, it returns true.

    let p = true;
    console.log(!p); // Outputs: false
    
    let q = false;
    console.log(!q); // Outputs: true
    
  4. Ternary operator: It’s often used as a shorthand for simple if...else statements.

    1. condition ? expression1 : expression2

    let age = 20;
    let message = age >= 18 ? "You are an adult" : "You are a minor";
    console.log(message); // Outputs: "You are an adult"
    

5.7. Empty values#

There are two special values, written null and undefined, that are used to denote the absence of a meaningful value.

  • Many operations in the language that don’t produce a meaningful value yield undefined simply because they have to yield some value.

5.8. Automatic type conversion#

console.log(8 * null);
// → 0
console.log("5" - 1);
// → 4
console.log("5" + 1);
// → 51
console.log("five" * 2);
// → NaN
console.log(false == 0);
// → true
console.log(null == undefined);
// → true
console.log(null == 0);
// → false
console.log(null || "user");
// → user
console.log("Agnes" || "user");
// → Agnes