{ }

Intro to JavaScript

The language of the web. Every website you've ever used runs JavaScript.

1

The Story of JavaScript

Born in a rush, named as a trick, and now the most widely used programming language in the world. Here is how it happened.

1995
Brendan Eich creates the language in just 10 days at Netscape. It was originally called Mocha.
1995 (later)
Renamed to LiveScript, then quickly to JavaScript as a marketing move. Java was the hottest language at the time, so the name was chosen to ride that wave. The two languages are actually very different.
1997
JavaScript is standardized as ECMAScript. This ensures every browser runs it the same way.
2009
Node.js is released, letting JavaScript run outside the browser on servers.
2015
ES6 (ES2015) modernizes the language with let/const, arrow functions, classes, template literals, and more.
Today
JavaScript runs in every browser, on servers (Node.js), in mobile apps (React Native), desktop apps (Electron), and even on robots and IoT devices. It is the most popular language on GitHub.
Fun Fact

Despite the name, JavaScript has almost nothing to do with Java. The naming was purely a marketing decision by Netscape to capitalize on Java's popularity in 1995.

2

Hello, World!

Every programmer starts here. In JavaScript, you print to the console with console.log(). Let's also look at variables and template literals.

// Your first JavaScript program console.log('Hello, World!'); // Variables: use const for values that don't change const name = 'Alice'; const age = 14; // Use let for values that can change let score = 0; score = score + 10; // Template literals: embed variables inside strings with backticks console.log(`Hi, I'm ${name} and I'm ${age} years old`); console.log(`My score is ${score}`);
// REPL: try these one at a time
> console.log('Hello, World!')
Hello, World!
> 2 + 2
4
> 'hello'.toUpperCase()
'HELLO'

const vs let

const declares a variable that cannot be reassigned. let declares one that can. Use const by default, let when you need to change the value.

Template Literals

Use backticks (`) instead of quotes and ${expression} to embed values directly in a string. Much cleaner than string concatenation.

Semicolons

Semicolons at the end of statements are optional in JavaScript (the engine inserts them automatically), but many developers include them for clarity.

3

Arrays and Objects

JavaScript's two core data structures. Arrays hold ordered lists of values. Objects hold named key-value pairs. Together they can represent almost anything.

// Arrays: ordered lists const colors = ['red', 'green', 'blue']; console.log(colors[0]); // 'red' (arrays start at index 0) console.log(colors.length); // 3 // Useful array methods colors.push('yellow'); // add to end colors.pop(); // remove last item // map: transform every item const upper = colors.map(c => c.toUpperCase()); // ['RED', 'GREEN', 'BLUE'] // filter: keep only items that pass a test const short = colors.filter(c => c.length <= 4); // ['red', 'blue']
// Objects: named key-value pairs const player = { name: 'Alice', health: 100, inventory: ['sword', 'shield'], isAlive: true }; console.log(player.name); // 'Alice' console.log(player.inventory[0]); // 'sword' // JSON: JavaScript Object Notation // Objects convert to JSON for storage and transmission const json = JSON.stringify(player); const parsed = JSON.parse(json);

JSON: The Universal Data Format

JSON (JavaScript Object Notation) was born from JavaScript objects, but it is now used by almost every programming language. APIs, config files, databases: JSON is everywhere. If you learn JavaScript objects, you already know JSON.

map()

Creates a new array by transforming every element. The original array stays unchanged.

filter()

Creates a new array with only the elements that pass a condition. Returns true to keep, false to skip.

4

Functions and Arrow Functions

Functions are reusable blocks of code. JavaScript has two syntaxes for writing them, and both are used everywhere.

// Classic function declaration function greet(name) { return `Hello, ${name}!`; } console.log(greet('Alice')); // 'Hello, Alice!' // Arrow function (shorter syntax, same idea) const greet2 = (name) => { return `Hello, ${name}!`; }; // Even shorter: one-line arrow functions const double = (n) => n * 2; const isEven = (n) => n % 2 === 0; console.log(double(5)); // 10 console.log(isEven(4)); // true
// Callbacks: passing functions as arguments const numbers = [1, 2, 3, 4, 5]; // forEach: run a function on each item numbers.forEach(n => console.log(n * 10)); // 10, 20, 30, 40, 50 // map + arrow function = very common pattern const doubled = numbers.map(n => n * 2); // [2, 4, 6, 8, 10] // Chaining: combine operations const result = numbers .filter(n => n > 2) .map(n => n * 10); // [30, 40, 50]

function Keyword

The original syntax. Hoisted to the top of their scope, so you can call them before they appear in the code.

Arrow Functions (=>)

Introduced in ES6. Shorter syntax, especially for one-liners. Perfect for callbacks and array methods.

Callbacks

A function passed as an argument to another function. This is the foundation of asynchronous JavaScript and event handling.

Fun Fact

In JavaScript, functions are "first-class citizens." That means you can assign them to variables, pass them as arguments, and return them from other functions, just like any other value.

5

What Makes JavaScript Special

Plenty of languages can do variables, arrays, and functions. What sets JavaScript apart from every other language?

Runs Everywhere

Browser, server (Node.js), mobile (React Native), desktop (Electron), embedded devices. No other language matches this reach.

Event-Driven

JavaScript listens for events: clicks, key presses, network responses, timers. Your code reacts when things happen, instead of running line by line.

Asynchronous

JavaScript can start a task (like fetching data), move on to other work, and come back when the data arrives. This makes it great for web apps.

The npm Ecosystem

npm (Node Package Manager) has over 2 million packages. Need a date library, a game engine, an image processor? Someone already built it.

The Only Browser Language

JavaScript is the only programming language that every web browser can run natively. HTML and CSS handle structure and style. JavaScript handles behavior.

Prototype-Based OOP

Instead of traditional classes (though JS has those too), objects can inherit directly from other objects. This is a flexible, powerful approach to code reuse.

// Event-driven: respond to user actions document.querySelector('button').addEventListener('click', () => { console.log('Button clicked!'); }); // Asynchronous: fetch data from an API const response = await fetch('https://api.example.com/data'); const data = await response.json(); console.log(data);
Fun Fact

Every major tech company uses JavaScript extensively. Netflix, Uber, PayPal, LinkedIn, and NASA all run JavaScript in production. The language that was built in 10 days now powers most of the internet.

6

Try It Yourself

The best way to learn is by writing code. Here is how to get started with JavaScript in the terminal right now.

apt install node

Install Node.js in the terminal. This lets you run JavaScript outside a browser.

nano app.js

Create a new file called app.js and open the text editor.

node app.js

Run your JavaScript file. Output appears in the terminal.

node

Start the Node.js REPL. Type JavaScript expressions and see results instantly. Press Ctrl+C to exit.

Mini-Project: Build a Greeting Generator

Open the terminal, install Node, and create this file:

// greeting.js const greetings = [ 'Hello', 'Howdy', 'Hey there', 'Greetings', 'Yo' ]; const names = ['World', 'JavaScript', 'Developer']; const randomPick = (arr) => arr[Math.floor(Math.random() * arr.length)]; for (let i = 0; i < 5; i++) { console.log(`${randomPick(greetings)}, ${randomPick(names)}!`); }
# Run it: $ node greeting.js Hey there, Developer! Yo, World! Hello, JavaScript! Greetings, Developer! Howdy, World!
What's Next?

Once you are comfortable with the basics, explore the DOM (Document Object Model) to make web pages interactive, or try building a simple server with Node.js. JavaScript opens doors to everything on the web.

JS Developer!

You now know the fundamentals of JavaScript, the most widely used programming language on Earth. Variables, functions, arrays, objects: these are the building blocks of every web app, game, and server.

0
Sections Explored
0
Time Exploring

Variables Hold Data

Use let and const to store values. const for things that never change, let for things that do.

Functions Are Reusable Code

Wrap logic in a function and call it by name. Arrow functions (=>) make this even shorter.

Arrays and Objects Organize Data

Arrays are ordered lists. Objects are named collections. Together they can model anything.

JS Runs Everywhere

Browsers, servers, mobile apps, desktop apps, robots. No other language has this reach.

Ready to Create?

Put your new knowledge into practice!

Suggest a Correction