Introduction to JavaScript
Learning Objectives
- By the end of this lesson, you will be able to:
- - Understand what JavaScript is and its key characteristics
- - Explain the history and evolution of JavaScript
- - Distinguish between JavaScript in the browser and Node.js
- - Set up a development environment for JavaScript
- - Use the browser console and DevTools
- - Write and run your first JavaScript program
Lesson 1.1: Introduction to JavaScript
Learning Objectives
By the end of this lesson, you will be able to:
- Understand what JavaScript is and its key characteristics
- Explain the history and evolution of JavaScript
- Distinguish between JavaScript in the browser and Node.js
- Set up a development environment for JavaScript
- Use the browser console and DevTools
- Write and run your first JavaScript program
What is JavaScript?
JavaScript is a high-level, interpreted programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. Created by Brendan Eich in 1995, JavaScript has evolved from a simple scripting language for web pages into a powerful, versatile language used for both frontend and backend development.
Key Characteristics of JavaScript
-
Interpreted Language: JavaScript code is executed directly by a JavaScript engine (like V8 in Chrome) without needing compilation. However, modern engines use Just-In-Time (JIT) compilation for performance.
-
Dynamic Typing: Variables don't need explicit type declarations. JavaScript automatically determines the type based on the value assigned.
-
Prototype-Based: JavaScript uses prototypes for inheritance rather than traditional classes (though ES6 introduced class syntax).
-
First-Class Functions: Functions in JavaScript are treated as objects - they can be assigned to variables, passed as arguments, and returned from other functions.
-
Event-Driven: JavaScript is designed to respond to user interactions and browser events, making it ideal for interactive web applications.
-
Single-Threaded with Asynchronous Capabilities: JavaScript runs on a single thread but uses an event loop and callbacks to handle asynchronous operations.
-
Cross-Platform: JavaScript runs in browsers (client-side) and on servers (Node.js), making it truly universal.
JavaScript's Role in Web Development
JavaScript is one of the three core web technologies:
- HTML: Structure and content
- CSS: Styling and layout
- JavaScript: Behavior and interactivity
Together, they create modern, dynamic web experiences.
JavaScript History and Evolution
The Early Years (1995-2000)
1995 - Birth of JavaScript
- Created by Brendan Eich at Netscape in just 10 days
- Originally named "Mocha," then "LiveScript," finally "JavaScript"
- Designed to add interactivity to static HTML pages
- Released with Netscape Navigator 2.0
1996 - Standardization
- JavaScript was submitted to ECMA International for standardization
- Became ECMAScript (ES1) - the official standard
- Microsoft created JScript for Internet Explorer (similar but not identical)
1997-1999 - ECMAScript 2 & 3
- ES2 (1998): Minor updates
- ES3 (1999): Added regular expressions, better string handling, try/catch
- ES3 became the dominant version for over a decade
The Dark Ages (2000-2009)
- Browser wars led to compatibility issues
- Different browsers implemented JavaScript differently
- Developers had to write browser-specific code
- jQuery emerged (2006) to solve cross-browser compatibility
The Renaissance (2009-2015)
2009 - Node.js
- Ryan Dahl created Node.js, bringing JavaScript to the server
- Enabled JavaScript to run outside browsers
- Revolutionized backend development
2015 - ECMAScript 6 (ES6/ES2015)
- Major update after 6 years
- Introduced: classes, arrow functions, let/const, modules, promises, and more
- Modern JavaScript as we know it today
Modern Era (2015-Present)
Annual Releases
- ES2016, ES2017, ES2018, ES2019, ES2020, ES2021, ES2022, ES2023, ES2024
- Regular updates with new features
- Async/await, optional chaining, nullish coalescing, and more
Current State
- JavaScript is the most popular programming language (GitHub, Stack Overflow)
- Used in web, mobile (React Native), desktop (Electron), IoT, and more
- Massive ecosystem with npm (largest package registry)
JavaScript in the Browser vs Node.js
Browser JavaScript (Client-Side)
JavaScript in the browser runs on the user's computer and interacts with:
Capabilities:
- Manipulate the DOM (Document Object Model)
- Handle user events (clicks, keyboard input, etc.)
- Make HTTP requests (AJAX/Fetch)
- Store data locally (localStorage, sessionStorage)
- Access browser APIs (Geolocation, Camera, etc.)
- Create animations and visual effects
Limitations:
- Cannot access the file system (security)
- Cannot make direct network connections (same-origin policy)
- Limited by browser security sandbox
Example Use Cases:
- Interactive web pages
- Form validation
- Dynamic content updates
- Single Page Applications (SPAs)
- Games and animations
Node.js (Server-Side)
Node.js allows JavaScript to run on servers and provides:
Capabilities:
- File system access
- Network operations
- Database connections
- Server-side logic
- API development
- Command-line tools
Differences:
- No DOM (no
documentorwindowobjects) - Different global objects (
globalinstead ofwindow) - Can use CommonJS modules (
require/module.exports) - Access to system resources
Example Use Cases:
- Web servers (Express.js)
- REST APIs
- Real-time applications (WebSockets)
- Build tools and automation
- Desktop applications (Electron)
Key Similarities
Both environments share:
- Core JavaScript language features
- Same syntax and data types
- Same control structures (if/else, loops, functions)
- Modern ES6+ features
Setting Up Your Development Environment
1. Web Browser
Any modern browser works, but recommended:
- Google Chrome (Recommended) - Best DevTools
- Mozilla Firefox - Excellent developer tools
- Microsoft Edge - Good DevTools, Chromium-based
- Safari - Good for macOS/iOS testing
2. Code Editor
Choose a code editor that fits your needs:
Visual Studio Code (VS Code) (Highly Recommended)
- Free and open-source
- Excellent JavaScript support
- Built-in terminal
- Extensions available
- Download: code.visualstudio.com
Other Options:
- Sublime Text: Lightweight and fast
- Atom: Open-source, customizable
- WebStorm: Full-featured IDE (paid)
- Notepad++: Simple text editor (Windows)
3. Installing VS Code Extensions
Recommended extensions for JavaScript:
- JavaScript (ES6) code snippets - Code completion
- Prettier - Code formatter
- ESLint - Code linting
- Live Server - Local development server
- JavaScript Debugger - Built-in debugging
How to Install:
- Open VS Code
- Click Extensions icon (or
Ctrl+Shift+X/Cmd+Shift+X) - Search for extension name
- Click Install
4. Node.js (Optional but Recommended)
Node.js is useful even for frontend development (build tools, package managers).
Installation:
Windows/macOS:
- Visit nodejs.org
- Download LTS (Long Term Support) version
- Run installer
- Verify: Open terminal and type
node --version
Linux:
# Ubuntu/Debian
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
# Verify
node --version
npm --version
Verify Installation:
node --version # Should show v20.x.x or similar
npm --version # Should show 10.x.x or similar
5. Project Structure
Create a folder for your JavaScript projects:
# Create project folder
mkdir javascript-learning
cd javascript-learning
# Create your first file
touch index.html
touch script.js
Browser Console and DevTools
The browser console is your best friend for learning JavaScript. It allows you to:
- Run JavaScript code immediately
- See errors and debug issues
- Inspect variables and objects
- Test code snippets
Opening DevTools
Chrome/Edge:
- Press
F12 - Or
Ctrl+Shift+I(Windows/Linux) - Or
Cmd+Option+I(macOS) - Or Right-click → Inspect
Firefox:
- Press
F12 - Or
Ctrl+Shift+I(Windows/Linux) - Or
Cmd+Option+I(macOS)
Safari:
- Enable Developer menu: Preferences → Advanced → "Show Develop menu"
- Then
Cmd+Option+I
Console Tab
The Console tab is where you'll spend most of your time:
Basic Usage:
// Type JavaScript code and press Enter
console.log("Hello, World!");
// Perform calculations
2 + 2;
// Create variables
let name = "JavaScript";
console.log(name);
Console Methods
console.log() - General output
console.log("Hello");
console.log(42);
console.log(true);
console.error() - Error messages
console.error("Something went wrong!");
console.warn() - Warning messages
console.warn("This is a warning");
console.info() - Informational messages
console.info("This is information");
console.table() - Display data as table
let users = [
{name: "Alice", age: 25},
{name: "Bob", age: 30}
];
console.table(users);
Other DevTools Tabs
- Elements: Inspect HTML and CSS
- Network: Monitor network requests
- Sources: Debug JavaScript files
- Application: View storage, cookies, etc.
- Performance: Analyze performance
Tips for Using Console
- Clear Console: Type
clear()orCtrl+L - Multi-line Code: Press
Shift+Enterfor new line - Command History: Use up/down arrows
- Auto-completion: Press
Tabfor suggestions
Your First JavaScript Program
Method 1: Browser Console (Quickest)
- Open Browser Console (F12)
- Type in Console:
console.log("Hello, World!"); - Press Enter
- See Output:
Hello, World!
Method 2: HTML File with Inline Script
-
Create
index.html:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My First JavaScript</title> </head> <body> <h1>Hello, World!</h1> <script> console.log("Hello from JavaScript!"); alert("Welcome to JavaScript!"); </script> </body> </html> -
Open in Browser: Double-click the file or right-click → Open with browser
-
Check Console: Open DevTools (F12) to see the console output
Method 3: External JavaScript File (Recommended)
-
Create
index.html:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My First JavaScript</title> </head> <body> <h1>Hello, World!</h1> <p id="output"></p> <script src="script.js"></script> </body> </html> -
Create
script.js:// My first JavaScript program console.log("Hello, World!"); // Display message on page document.getElementById("output").textContent = "JavaScript is working!"; // Alert message alert("Welcome to JavaScript!"); -
Open
index.htmlin browser
Method 4: Node.js (Command Line)
-
Create
hello.js:console.log("Hello, World!"); -
Run in Terminal:
node hello.js -
Output:
Hello, World!
Understanding the Code
Let's break down the basic example:
console.log("Hello, World!");
console- A global object that provides access to the browser console.log()- A method that outputs messages to the console"Hello, World!"- A string (text) enclosed in quotation marks;- Semicolon (optional in JavaScript but recommended)
Variations to Try
// Simple message
console.log("Hello, World!");
// Multiple messages
console.log("Hello");
console.log("World");
// Numbers
console.log(42);
console.log(3.14);
// Calculations
console.log(2 + 2);
console.log(10 * 5);
// Variables
let message = "Hello, JavaScript!";
console.log(message);
// Multiple values
console.log("Name:", "Alice", "Age:", 25);
Practice Exercise
Exercise: Write and Run Your First Program
Objective: Create a JavaScript program that introduces yourself and demonstrates basic console usage.
Instructions:
-
Create a new HTML file called
introduction.html -
Create a JavaScript file called
introduction.js -
Write a program that:
- Prints your name to the console
- Prints your favorite programming language
- Performs a simple calculation (your age in days: age × 365)
- Displays a message on the webpage
- Uses at least 3 different console methods
-
Open the HTML file in a browser
-
Check the console (F12) to see your output
Example Solution:
introduction.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Introduction</title>
</head>
<body>
<h1>Welcome!</h1>
<div id="info"></div>
<script src="introduction.js"></script>
</body>
</html>
introduction.js:
// Introduction Program
let name = "Your Name";
let age = 25;
let favoriteLanguage = "JavaScript";
// Console output
console.log("=== Introduction ===");
console.log("Name:", name);
console.log("Favorite Language:", favoriteLanguage);
console.log("Age:", age);
console.log("Age in days:", age * 365);
// Display on page
document.getElementById("info").innerHTML = `
<p><strong>Name:</strong> ${name}</p>
<p><strong>Age:</strong> ${age}</p>
<p><strong>Favorite Language:</strong> ${favoriteLanguage}</p>
`;
// Warning
console.warn("This is my first JavaScript program!");
// Info
console.info("JavaScript is awesome!");
Expected Output in Console:
=== Introduction ===
Name: Your Name
Favorite Language: JavaScript
Age: 25
Age in days: 9125
⚠ This is my first JavaScript program!
ℹ JavaScript is awesome!
Challenge (Optional):
- Add more information about yourself
- Use
console.table()to display your information - Create multiple variables and combine them
- Try different console methods
Key Takeaways
- JavaScript is a versatile programming language for web development
- History: Created in 1995, evolved through ES6 (2015) to modern JavaScript
- Two Environments: Browser (client-side) and Node.js (server-side)
- DevTools: Essential for debugging and testing JavaScript
- console.log(): Primary method for outputting information
- Three Ways to Run: Browser console, HTML with script, or Node.js
- External Files: Best practice is to use separate
.jsfiles
Common Mistakes to Avoid
- Forgetting to Open Console: Always check DevTools console for output
- Syntax Errors: Missing quotes, parentheses, or brackets
- File Path Issues: Make sure script file path is correct in HTML
- Not Saving Files: Always save before refreshing browser
- Case Sensitivity: JavaScript is case-sensitive (
consolenotConsole)
Quiz: JavaScript Basics
Test your understanding with these questions:
-
What is JavaScript?
- A) A coffee brand
- B) A programming language for web development
- C) A database system
- D) An operating system
-
When was JavaScript created?
- A) 1990
- B) 1995
- C) 2000
- D) 2010
-
What does console.log() do?
- A) Creates a new file
- B) Displays output in the browser console
- C) Performs calculations
- D) Styles web pages
-
Which of these is NOT a way to run JavaScript?
- A) Browser console
- B) HTML file with script tag
- C) Node.js
- D) Python interpreter
-
True or False: JavaScript can only run in browsers.
- A) True
- B) False (Node.js allows server-side JavaScript)
-
What key opens DevTools in most browsers?
- A) F1
- B) F12
- C) F5
- D) Ctrl+S
-
What is the file extension for JavaScript files?
- A) .java
- B) .js
- C) .javascript
- D) .script
Answers:
- B) A programming language for web development
- B) 1995
- B) Displays output in the browser console
- D) Python interpreter
- B) False (Node.js allows server-side JavaScript)
- B) F12
- B) .js
Next Steps
Congratulations! You've completed your first JavaScript lesson. You now know:
- What JavaScript is and its history
- How JavaScript works in browsers vs Node.js
- How to set up a development environment
- How to use browser DevTools and console
- How to write and run your first JavaScript program
What's Next?
- Lesson 1.2: JavaScript Syntax and Basics
- Practice writing more console programs
- Experiment with different console methods
- Explore DevTools features
- Try creating interactive web pages
Additional Resources
- MDN Web Docs: developer.mozilla.org/en-US/docs/Web/JavaScript
- JavaScript.info: javascript.info
- Node.js Documentation: nodejs.org/docs
- Chrome DevTools Guide: developer.chrome.com/docs/devtools
- JavaScript History: en.wikipedia.org/wiki/JavaScript
Lesson completed! You're ready to move on to the next lesson.
Course Navigation
- Introduction to JavaScript
- JavaScript Syntax and Basics
- Variables and Data Types