JavaScript Overview

Printing

Statements

Syntax

Comments

Variables

JavaScript Let

Block Scope

Global Scope

Cannot be Redeclared

Redeclaring Variables

Difference Between var, let, and const

Scope Redeclare Reassign Hoisted Binds this
var Yes Yes Yes Yes
let No No Yes No
const No No No No

What is Good?

What is Not Good?

JavaScript Const

Cannot be Reassigned

const PI = 3.14159;
// PI = 3.14;  // Error
// PI = PI + 10;  // Error
    

Must be Assigned

When to Use

Constant Arrays & Objects

Summary

Primitive Data Types

In JavaScript, primitive data types are the most basic types of data. They are immutable and are not objects. These include:

Example Code:


let a = null;                // Null type
let b = 345;                 // Number type
let c = true;                // Boolean type
let d = BigInt("456") + BigInt(3); // BigInt type
let e = "harry";             // String type
let f = Symbol("I am good"); // Symbol type
let g = undefined;           // Undefined type
let h;                       // Will be undefined by default

console.log(a, b, c, d, e, f, g, h); // Outputs: null 345 true 459n 'harry' Symbol(I am good) undefined undefined
console.log(typeof d); // Outputs: 'bigint'
    

Non-Primitive Data Types

Objects in JavaScript are non-primitive data types used to store collections of data and more complex entities. Objects are key-value pairs and can be compared to dictionaries in Python.

Example of an Object:


const item = {
  "array": true,
  "rohn": 56
};

console.log(item["Harray"]); // This will return `undefined` because the key "Harray" does not exist in the `item` object.
    

Analysis:

Key Points:

Expressions and Operators

An expression is a fragment of code that produces a value. For example, 7; is an expression, as well as 7 + 5, which evaluates to 12. In this example, 7 and 5 are operands, and + is the operator. An operator works with one or more operands.

Every value that returns literally is an expression.

Types of Operators in JavaScript:

1. Arithmetic Operators

2. Assignment Operators

3. Comparison Operators

4. Ternary Operator

condition ? expression1 : expression2

Used as a shorthand for if-else statements.

5. Logical Operators

Logical operators are used to compare two or more conditions: