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

  1. 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.

  2. Dynamic Typing: Variables don't need explicit type declarations. JavaScript automatically determines the type based on the value assigned.

  3. Prototype-Based: JavaScript uses prototypes for inheritance rather than traditional classes (though ES6 introduced class syntax).

  4. First-Class Functions: Functions in JavaScript are treated as objects - they can be assigned to variables, passed as arguments, and returned from other functions.

  5. Event-Driven: JavaScript is designed to respond to user interactions and browser events, making it ideal for interactive web applications.

  6. Single-Threaded with Asynchronous Capabilities: JavaScript runs on a single thread but uses an event loop and callbacks to handle asynchronous operations.

  7. 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 document or window objects)
  • Different global objects (global instead of window)
  • 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:

  1. JavaScript (ES6) code snippets - Code completion
  2. Prettier - Code formatter
  3. ESLint - Code linting
  4. Live Server - Local development server
  5. JavaScript Debugger - Built-in debugging

How to Install:

  1. Open VS Code
  2. Click Extensions icon (or Ctrl+Shift+X / Cmd+Shift+X)
  3. Search for extension name
  4. Click Install

4. Node.js (Optional but Recommended)

Node.js is useful even for frontend development (build tools, package managers).

Installation:

Windows/macOS:

  1. Visit nodejs.org
  2. Download LTS (Long Term Support) version
  3. Run installer
  4. 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

  1. Clear Console: Type clear() or Ctrl+L
  2. Multi-line Code: Press Shift+Enter for new line
  3. Command History: Use up/down arrows
  4. Auto-completion: Press Tab for suggestions

Your First JavaScript Program

Method 1: Browser Console (Quickest)

  1. Open Browser Console (F12)
  2. Type in Console:
    console.log("Hello, World!");
  3. Press Enter
  4. See Output:
    Hello, World!

Method 2: HTML File with Inline Script

  1. 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>
  2. Open in Browser: Double-click the file or right-click → Open with browser

  3. Check Console: Open DevTools (F12) to see the console output

Method 3: External JavaScript File (Recommended)

  1. 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>
  2. 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!");
  3. Open index.html in browser

Method 4: Node.js (Command Line)

  1. Create hello.js:

    console.log("Hello, World!");
  2. Run in Terminal:

    node hello.js
  3. 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:

  1. Create a new HTML file called introduction.html

  2. Create a JavaScript file called introduction.js

  3. 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
  4. Open the HTML file in a browser

  5. 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

  1. JavaScript is a versatile programming language for web development
  2. History: Created in 1995, evolved through ES6 (2015) to modern JavaScript
  3. Two Environments: Browser (client-side) and Node.js (server-side)
  4. DevTools: Essential for debugging and testing JavaScript
  5. console.log(): Primary method for outputting information
  6. Three Ways to Run: Browser console, HTML with script, or Node.js
  7. External Files: Best practice is to use separate .js files

Common Mistakes to Avoid

  1. Forgetting to Open Console: Always check DevTools console for output
  2. Syntax Errors: Missing quotes, parentheses, or brackets
  3. File Path Issues: Make sure script file path is correct in HTML
  4. Not Saving Files: Always save before refreshing browser
  5. Case Sensitivity: JavaScript is case-sensitive (console not Console)

Quiz: JavaScript Basics

Test your understanding with these questions:

  1. What is JavaScript?

    • A) A coffee brand
    • B) A programming language for web development
    • C) A database system
    • D) An operating system
  2. When was JavaScript created?

    • A) 1990
    • B) 1995
    • C) 2000
    • D) 2010
  3. What does console.log() do?

    • A) Creates a new file
    • B) Displays output in the browser console
    • C) Performs calculations
    • D) Styles web pages
  4. 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
  5. True or False: JavaScript can only run in browsers.

    • A) True
    • B) False (Node.js allows server-side JavaScript)
  6. What key opens DevTools in most browsers?

    • A) F1
    • B) F12
    • C) F5
    • D) Ctrl+S
  7. What is the file extension for JavaScript files?

    • A) .java
    • B) .js
    • C) .javascript
    • D) .script

Answers:

  1. B) A programming language for web development
  2. B) 1995
  3. B) Displays output in the browser console
  4. D) Python interpreter
  5. B) False (Node.js allows server-side JavaScript)
  6. B) F12
  7. 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


Lesson completed! You're ready to move on to the next lesson.