Docs

Module-01-JavaScript-Fundamentals

1.1 Introduction to JavaScript

šŸ“š Table of Contents

  1. •What is JavaScript?
  2. •Where JavaScript Runs
  3. •JavaScript Engines
  4. •ECMAScript Standards
  5. •How JavaScript Executes in the Browser
  6. •Chrome DevTools Basics

What is JavaScript?

JavaScript is a high-level, interpreted, dynamic programming language that was created in 1995 by Brendan Eich at Netscape. It was originally designed to add interactivity to web pages.

Key Characteristics of JavaScript:

CharacteristicDescription
High-levelYou don't need to manage memory or hardware directly
InterpretedCode is executed line by line (though modern engines compile it)
DynamicVariables can hold any type of data
Weakly typedTypes are determined at runtime, not compile time
Multi-paradigmSupports procedural, object-oriented, and functional programming
Single-threadedExecutes one operation at a time (but can handle async operations)

What JavaScript Can Do:

  1. •Manipulate web page content - Change HTML and CSS dynamically
  2. •Respond to user actions - Handle clicks, keyboard input, form submissions
  3. •Validate forms - Check user input before sending to server
  4. •Create animations - Smooth transitions and visual effects
  5. •Make network requests - Fetch data from servers without page reload
  6. •Store data locally - Save information in the browser

What JavaScript Is NOT:

  • •JavaScript is NOT Java (despite the similar name)
  • •JavaScript is NOT just for web pages (but we focus on browser JS here)
  • •JavaScript is NOT a compiled language (traditionally)

Where JavaScript Runs

In this course, we focus exclusively on browser-based JavaScript.

The Browser Environment

When you open a web page, the browser creates an environment where JavaScript can run:

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│              BROWSER WINDOW                 │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│  ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”   │
│  │           Web Page (DOM)            │   │
│  │   ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”   │   │
│  │   │     JavaScript Engine       │   │   │
│  │   │   (Runs your JS code)       │   │   │
│  │   ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜   │   │
│  ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜   │
│                                             │
│  Browser APIs: DOM, Fetch, Storage, etc.   │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Three Ways to Add JavaScript to a Web Page:

1. Inline JavaScript (Not Recommended)

<button onclick="alert('Hello!')">Click Me</button>

2. Internal Script (Script Tag)

<!DOCTYPE html>
<html>
<head>
    <title>My Page</title>
</head>
<body>
    <h1>Hello World</h1>
    
    <script>
        // Your JavaScript code here
        console.log("Hello from JavaScript!");
    </script>
</body>
</html>

3. External Script (Recommended)

<!DOCTYPE html>
<html>
<head>
    <title>My Page</title>
</head>
<body>
    <h1>Hello World</h1>
    
    <script src="script.js"></script>
</body>
</html>

Script Placement Best Practices:

PlacementWhen to Use
End of <body>Default choice - HTML loads first
<head> with deferScript loads parallel, runs after HTML
<head> with asyncScript loads parallel, runs immediately when ready
<!-- defer: Loads parallel to HTML, executes after DOM is ready -->
<script src="script.js" defer></script>

<!-- async: Loads parallel, executes as soon as downloaded -->
<script src="script.js" async></script>

JavaScript Engines

A JavaScript engine is a program that interprets and executes JavaScript code. Every browser has its own engine.

Major JavaScript Engines:

EngineBrowserDeveloper
V8Chrome, Edge, OperaGoogle
SpiderMonkeyFirefoxMozilla
JavaScriptCore (Nitro)SafariApple
ChakraOld Edge (legacy)Microsoft

How JavaScript Engines Work:

Source Code → Parser → AST → Interpreter → Bytecode
                                  ↓
                           JIT Compiler
                                  ↓
                          Machine Code
  1. •Parsing: Reads code and creates Abstract Syntax Tree (AST)
  2. •Interpretation: Converts AST to bytecode
  3. •JIT Compilation: "Hot" code paths are compiled to machine code for speed
  4. •Execution: Runs the optimized code

V8 Engine Deep Dive:

V8 (used in Chrome) has two main components:

  1. •

    Ignition - The interpreter

    • •Converts JavaScript to bytecode
    • •Fast startup time
  2. •

    TurboFan - The optimizing compiler

    • •Compiles frequently-used code to machine code
    • •Makes "hot" functions run faster
JavaScript Source
       ↓
   [Parser]
       ↓
      AST
       ↓
   [Ignition]  ←──┐
       ↓         │ (Deoptimization if assumptions fail)
   Bytecode      │
       ↓         │
   [TurboFan] ā”€ā”€ā”€ā”˜
       ↓
  Machine Code

ECMAScript Standards

ECMAScript (ES) is the official specification that JavaScript follows. Think of ECMAScript as the "rulebook" and JavaScript as the "implementation."

Brief History:

VersionYearMajor Features
ES11997First edition
ES21998Editorial changes
ES31999Regular expressions, try/catch
ES52009Strict mode, JSON, Array methods
ES6/ES20152015Classes, modules, arrow functions, let/const, promises
ES20162016Array.includes(), exponentiation operator
ES20172017async/await, Object.entries()
ES20182018Rest/spread properties, async iteration
ES20192019Array.flat(), Object.fromEntries()
ES20202020Optional chaining, nullish coalescing, BigInt
ES20212021String.replaceAll(), Promise.any()
ES20222022Top-level await, private class fields
ES20232023Array find from last, hashbang grammar
ES20242024Object.groupBy, Promise.withResolvers

ES6 (ES2015) - The Game Changer

ES6 was the biggest update to JavaScript, introducing:

  • •let and const
  • •Arrow functions
  • •Classes
  • •Template literals
  • •Destructuring
  • •Modules (import/export)
  • •Promises
  • •Symbols
  • •Iterators and generators

Yearly Release Cycle

Since 2015, ECMAScript follows a yearly release cycle:

  • •New features are proposed through a TC39 process
  • •Features go through 5 stages (Stage 0 to Stage 4)
  • •Stage 4 features are included in the yearly release

TC39 Process Stages:

StageNameDescription
0StrawpersonInitial idea
1ProposalFormal proposal with examples
2DraftInitial spec language
3CandidateComplete spec, needs feedback
4FinishedReady for inclusion

How JavaScript Executes in the Browser

Understanding execution is crucial for debugging and writing efficient code.

The Execution Process:

1. Browser loads HTML
       ↓
2. Browser encounters <script> tag
       ↓
3. JavaScript engine takes over
       ↓
4. Code is parsed into AST
       ↓
5. Execution context is created
       ↓
6. Code is executed line by line
       ↓
7. Browser resumes HTML parsing

Execution Context:

Every time JavaScript runs, it creates an Execution Context:

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│       Global Execution Context      │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│  Variable Environment:              │
│    - var declarations               │
│    - function declarations          │
│                                     │
│  Lexical Environment:               │
│    - let/const declarations         │
│                                     │
│  this binding: window (in browser)  │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

The Call Stack:

JavaScript uses a Call Stack to keep track of function calls:

function first() {
    second();
}

function second() {
    third();
}

function third() {
    console.log("Hello");
}

first();

Call Stack visualization:

Step 1: [global]
Step 2: [first, global]
Step 3: [second, first, global]
Step 4: [third, second, first, global]
Step 5: [second, first, global]  // third() finished
Step 6: [first, global]          // second() finished
Step 7: [global]                 // first() finished
Step 8: []                       // global finished

Synchronous Execution:

JavaScript is single-threaded and synchronous by default:

console.log("First");   // Runs 1st
console.log("Second");  // Runs 2nd
console.log("Third");   // Runs 3rd

// Output:
// First
// Second
// Third

Chrome DevTools Basics

Chrome DevTools is your best friend for JavaScript development. Learn it well!

Opening DevTools:

MethodShortcut
Keyboard (Windows/Linux)F12 or Ctrl + Shift + I
Keyboard (Mac)Cmd + Option + I
Right-click menuRight-click → Inspect
Chrome menuMenu → More Tools → Developer Tools

DevTools Panels:

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│  Elements │ Console │ Sources │ Network │ Performance │ ... │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│                                                             │
│              Panel Content Area                             │
│                                                             │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Console Panel (Most Important for Learning):

The Console is where you'll spend most of your time learning JavaScript.

Console Methods:

// Basic logging
console.log("Regular message");
console.info("Info message");
console.warn("Warning message");
console.error("Error message");

// Grouping
console.group("Group Name");
console.log("Inside group");
console.groupEnd();

// Tables
console.table([{name: "John", age: 30}, {name: "Jane", age: 25}]);

// Timing
console.time("Timer");
// ... code to time
console.timeEnd("Timer");

// Counting
console.count("Label"); // Label: 1
console.count("Label"); // Label: 2

// Clearing
console.clear();

Using the Console as a Playground:

You can type JavaScript directly in the console:

> let x = 5
< undefined
> x + 10
< 15
> "Hello " + "World"
< "Hello World"

Sources Panel:

Used for debugging JavaScript:

  1. •Breakpoints: Pause code execution at specific lines
  2. •Step through code: Execute one line at a time
  3. •Watch expressions: Monitor variable values
  4. •Call stack: See the chain of function calls

Setting a Breakpoint:

  1. •Open Sources panel
  2. •Find your JavaScript file
  3. •Click the line number to add a breakpoint
  4. •Refresh the page
  5. •Execution pauses at the breakpoint

Debugger Statement:

You can also pause code programmatically:

function problematicFunction() {
    let x = 10;
    debugger; // Execution pauses here when DevTools is open
    x = x * 2;
    return x;
}

Network Panel:

Useful for:

  • •Seeing HTTP requests
  • •Checking response data
  • •Debugging API calls
  • •Monitoring load times

Console Shortcuts:

ShortcutAction
Ctrl + LClear console
↑ / ↓Navigate command history
TabAuto-complete
Shift + EnterMulti-line input
$_Last evaluated result
$0Currently selected element

šŸŽÆ Key Takeaways

  1. •JavaScript is a high-level, dynamic, interpreted language created for web interactivity
  2. •JavaScript runs in the browser (we focus on this) and requires no special setup
  3. •JavaScript engines (V8, SpiderMonkey) parse and execute your code
  4. •ECMAScript defines the standard - ES6/ES2015 was the biggest update
  5. •JavaScript executes synchronously using a call stack
  6. •Chrome DevTools is essential for learning and debugging

šŸ“– Further Reading


āž”ļø Next Topic

Continue to 1.2 Basic Syntax to learn the fundamental syntax rules of JavaScript!

Module 01 JavaScript Fundamentals - JavaScript Tutorial | DeepML