JavaScript Learning Guide

The programming language for interactive web content

What is JavaScript?

JavaScript is a high-level, interpreted programming language that conforms to the ECMAScript specification. It is a language that is also characterized as dynamic, weakly typed, prototype-based and multi-paradigm.

Key Features of JavaScript:

MDN JavaScript Documentation

JavaScript Syntax Basics

JavaScript syntax is the set of rules that define how JavaScript programs are constructed:

// Variables let message = "Hello, World!"; const pi = 3.14159; // Functions function greet(name) { return `Hello, ${name}!`; } // Output console.log(greet("Developer"));

Adding JavaScript to HTML

Inline JavaScript

Add JavaScript directly to HTML elements using event attributes:

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

Note: Inline JavaScript is not recommended for complex code as it mixes structure with behavior.

Internal JavaScript

Place JavaScript code inside a <script> element in the HTML document:

<!DOCTYPE html> <html> <head> <title>JavaScript Example</title> <script> function showAlert() { alert('Hello from internal JavaScript!'); } </script> </head> <body> <button onclick="showAlert()">Click Me</button> </body> </html>

External JavaScript

Link to an external JavaScript file (recommended approach):

// In your HTML file <head> <script src="script.js" defer></script> </head> // In script.js function showAlert() { alert('Hello from external JavaScript!'); } // Wait for DOM to load document.addEventListener('DOMContentLoaded', () => { const button = document.querySelector('button'); button.addEventListener('click', showAlert); });

External JavaScript is the most maintainable approach as it separates behavior from structure.

Essential JavaScript Concepts

Master these fundamental JavaScript concepts to build interactive web applications:

Variables & Data Types
let, const, var, strings, numbers, booleans, objects, arrays
Functions
Declarations, expressions, arrow functions, parameters, return values
Control Flow
if/else, switch, for loops, while loops, ternary operator
DOM Manipulation
Selecting elements, modifying content, handling events
Objects & Arrays
Creating, accessing, and manipulating complex data structures
Async JavaScript
Callbacks, promises, async/await, fetching data from APIs
ES6+ Features
Arrow functions, template literals, destructuring, modules
Error Handling
try/catch blocks, throwing custom errors

JavaScript Data Types

JavaScript has dynamic types and seven fundamental data types:

Type Example Description
Number 42, 3.14 Integer and floating point numbers
String "Hello" Textual data enclosed in quotes
Boolean true, false Logical true/false values
Null null Intentional absence of any value
Undefined undefined Variable that has not been assigned a value
Object {name: "John"} Collection of key-value pairs
Symbol Symbol('id') Unique and immutable primitive value

Interactive JavaScript Demo

Try these examples to see JavaScript in action:

DOM Manipulation

Click the buttons to change this text.

// JavaScript code for the demo function changeText() { const element = document.getElementById('demo-element'); element.innerHTML = 'Text changed with JavaScript!'; } function changeColor() { const element = document.getElementById('demo-element'); element.style.color = '#f7df1e'; element.style.backgroundColor = '#2c2c2c'; }

Simple Calculator

Result will appear here

JavaScript Console

The browser console is a powerful tool for testing JavaScript code and debugging:

> let message = "Hello, Console!";
> console.log(message);
Hello, Console!
> const numbers = [1, 2, 3, 4, 5];
> numbers.forEach(n => console.log(n * 2));
2
4
6
8
10

To open the console:

Modern JavaScript (ES6+)

ECMAScript 2015 (ES6) introduced many new features that modernized JavaScript:

// Arrow functions const add = (a, b) => a + b; // Template literals const name = "Sarah"; const greeting = `Hello, ${name}!`; // Destructuring const person = { firstName: 'John', age: 30 }; const { firstName, age } = person; // Spread operator const arr1 = [1, 2, 3]; const arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5] // Modules import { functionName } from './module.js'; export const myVariable = 42;

Learning Resources

1

MDN Web Docs

Comprehensive JavaScript documentation and tutorials.

Visit MDN
2

JavaScript.info

Modern JavaScript tutorial from the basics to advanced topics.

Visit JavaScript.info
3

FreeCodeCamp

Free interactive JavaScript courses and projects.

Start Learning
4

Eloquent JavaScript

Well-regarded book available free online with interactive examples.

Read the Book