Module-01-JavaScript-Fundamentals
1.1 Introduction to JavaScript
š Table of Contents
- ā¢What is JavaScript?
- ā¢Where JavaScript Runs
- ā¢JavaScript Engines
- ā¢ECMAScript Standards
- ā¢How JavaScript Executes in the Browser
- ā¢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:
| Characteristic | Description |
|---|---|
| High-level | You don't need to manage memory or hardware directly |
| Interpreted | Code is executed line by line (though modern engines compile it) |
| Dynamic | Variables can hold any type of data |
| Weakly typed | Types are determined at runtime, not compile time |
| Multi-paradigm | Supports procedural, object-oriented, and functional programming |
| Single-threaded | Executes one operation at a time (but can handle async operations) |
What JavaScript Can Do:
- ā¢Manipulate web page content - Change HTML and CSS dynamically
- ā¢Respond to user actions - Handle clicks, keyboard input, form submissions
- ā¢Validate forms - Check user input before sending to server
- ā¢Create animations - Smooth transitions and visual effects
- ā¢Make network requests - Fetch data from servers without page reload
- ā¢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:
| Placement | When to Use |
|---|---|
End of <body> | Default choice - HTML loads first |
<head> with defer | Script loads parallel, runs after HTML |
<head> with async | Script 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:
| Engine | Browser | Developer |
|---|---|---|
| V8 | Chrome, Edge, Opera | |
| SpiderMonkey | Firefox | Mozilla |
| JavaScriptCore (Nitro) | Safari | Apple |
| Chakra | Old Edge (legacy) | Microsoft |
How JavaScript Engines Work:
Source Code ā Parser ā AST ā Interpreter ā Bytecode
ā
JIT Compiler
ā
Machine Code
- ā¢Parsing: Reads code and creates Abstract Syntax Tree (AST)
- ā¢Interpretation: Converts AST to bytecode
- ā¢JIT Compilation: "Hot" code paths are compiled to machine code for speed
- ā¢Execution: Runs the optimized code
V8 Engine Deep Dive:
V8 (used in Chrome) has two main components:
- ā¢
Ignition - The interpreter
- ā¢Converts JavaScript to bytecode
- ā¢Fast startup time
- ā¢
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:
| Version | Year | Major Features |
|---|---|---|
| ES1 | 1997 | First edition |
| ES2 | 1998 | Editorial changes |
| ES3 | 1999 | Regular expressions, try/catch |
| ES5 | 2009 | Strict mode, JSON, Array methods |
| ES6/ES2015 | 2015 | Classes, modules, arrow functions, let/const, promises |
| ES2016 | 2016 | Array.includes(), exponentiation operator |
| ES2017 | 2017 | async/await, Object.entries() |
| ES2018 | 2018 | Rest/spread properties, async iteration |
| ES2019 | 2019 | Array.flat(), Object.fromEntries() |
| ES2020 | 2020 | Optional chaining, nullish coalescing, BigInt |
| ES2021 | 2021 | String.replaceAll(), Promise.any() |
| ES2022 | 2022 | Top-level await, private class fields |
| ES2023 | 2023 | Array find from last, hashbang grammar |
| ES2024 | 2024 | Object.groupBy, Promise.withResolvers |
ES6 (ES2015) - The Game Changer
ES6 was the biggest update to JavaScript, introducing:
- ā¢
letandconst - ā¢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:
| Stage | Name | Description |
|---|---|---|
| 0 | Strawperson | Initial idea |
| 1 | Proposal | Formal proposal with examples |
| 2 | Draft | Initial spec language |
| 3 | Candidate | Complete spec, needs feedback |
| 4 | Finished | Ready 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:
| Method | Shortcut |
|---|---|
| Keyboard (Windows/Linux) | F12 or Ctrl + Shift + I |
| Keyboard (Mac) | Cmd + Option + I |
| Right-click menu | Right-click ā Inspect |
| Chrome menu | Menu ā 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:
- ā¢Breakpoints: Pause code execution at specific lines
- ā¢Step through code: Execute one line at a time
- ā¢Watch expressions: Monitor variable values
- ā¢Call stack: See the chain of function calls
Setting a Breakpoint:
- ā¢Open Sources panel
- ā¢Find your JavaScript file
- ā¢Click the line number to add a breakpoint
- ā¢Refresh the page
- ā¢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:
| Shortcut | Action |
|---|---|
Ctrl + L | Clear console |
ā / ā | Navigate command history |
Tab | Auto-complete |
Shift + Enter | Multi-line input |
$_ | Last evaluated result |
$0 | Currently selected element |
šÆ Key Takeaways
- ā¢JavaScript is a high-level, dynamic, interpreted language created for web interactivity
- ā¢JavaScript runs in the browser (we focus on this) and requires no special setup
- ā¢JavaScript engines (V8, SpiderMonkey) parse and execute your code
- ā¢ECMAScript defines the standard - ES6/ES2015 was the biggest update
- ā¢JavaScript executes synchronously using a call stack
- ā¢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!