# Introduction

Course: [JavaScript](https://www.devtutorials.ai/learn/javascript) · Section: Basics
Source: https://www.devtutorials.ai/learn/javascript/basics/introduction

> What JavaScript is and why it matters, how HTML, CSS and JavaScript share a page, your first program with console.log(), client-side versus server-side code, and adding scripts to HTML.

## What Is JavaScript?

**JavaScript** = the programming language that makes a web page *do* things.

- **HTML** puts a button on the page.
- **CSS** makes the button look good.
- **JavaScript** decides what happens when someone presses it.

**Simple lesson:** HTML is the structure, CSS is the style, and JavaScript is the behaviour.

### A Real-World Example

On a shop page, you press **Add to cart**.
The cart count changes from 0 to 1 at once.
The page did not reload. JavaScript changed the number for you.

<figure className="ts-fig">
  <svg viewBox="0 0 560 170" role="img" aria-label="Three boxes: HTML builds a button, CSS colours it, JavaScript reacts to a click and the cart count goes up." width="100%">
    <rect x="0" y="0" width="560" height="170" rx="12" fill="#f8fafc" stroke="#e2e8f0" />
    <rect x="20" y="30" width="150" height="70" rx="8" fill="#fff7ed" stroke="#ea580c" />
    <text x="95" y="58" textAnchor="middle" fontSize="13" fill="#9a3412">HTML</text>
    <text x="95" y="80" textAnchor="middle" fontSize="11" fill="#9a3412">what is there</text>
    <rect x="205" y="30" width="150" height="70" rx="8" fill="#eff6ff" stroke="#2563eb" />
    <text x="280" y="58" textAnchor="middle" fontSize="13" fill="#1e40af">CSS</text>
    <text x="280" y="80" textAnchor="middle" fontSize="11" fill="#1e40af">how it looks</text>
    <rect x="390" y="30" width="150" height="70" rx="8" fill="#fefce8" stroke="#ca8a04" />
    <text x="465" y="58" textAnchor="middle" fontSize="13" fill="#854d0e">JavaScript</text>
    <text x="465" y="80" textAnchor="middle" fontSize="11" fill="#854d0e">what it does</text>
    <rect x="390" y="118" width="150" height="34" rx="17" fill="#2563eb" />
    <text x="465" y="140" textAnchor="middle" fontSize="12" fill="#ffffff">Cart: 0 → 1</text>
    <circle r="7" cx="465" cy="108" fill="#ca8a04">
      <animate attributeName="cy" values="100;118;100" dur="2s" repeatCount="indefinite" />
    </circle>
  </svg>
  <figcaption>Each language has one job. JavaScript is the one that reacts.</figcaption>
</figure>

Every code box here is a small editor. Edit the code, press **Run** (or Ctrl+Enter), and read the **Console**.

Every box also has a small shop's practice data ready: `customers`, `products`, `orders`, `payments` and `events`. Later lessons use it.

## What You Will Learn

1. What JavaScript is, and why people learn it.
2. How HTML, CSS and JavaScript share a page.
3. How to print values with `console.log()`.
4. Where JavaScript runs: the browser and the server.
5. How to add JavaScript to an HTML page.

## Introduction to JavaScript

A program is a list of instructions.
JavaScript runs them **one by one, from top to bottom**.

```js runnable title="Three instructions, run from top to bottom"
const price = 25;
const discount = 5;
console.log("You pay:", price - discount);
```

Expected output:

```text
You pay: 20
```

### Why Learn JavaScript?

- Every web browser understands it. No install is needed.
- It also runs on servers, phones and desktops.
- One language takes you from a small page to a full app.

### Features of JavaScript

- **Interpreted:** the browser runs your code directly.
- **Dynamically typed:** the value decides its type, not the name.
- **Event-driven:** code can wait for a click, then react.
- **Asynchronous:** it can wait for slow work (like loading data) without freezing.

```js runnable title="The same name holds a number, then some text"
let seats = 24;
console.log(seats, typeof seats);

seats = "twenty-four";
console.log(seats, typeof seats);
```

Expected output:

```text
24 number
twenty-four string
```

`typeof` tells you the type of a value. The name `seats` held a number, then text, and nothing complained.

## Roles

### HTML, CSS, and JavaScript Relationship

| Language | Job | Shop example |
|---|---|---|
| HTML | Structure | The **Add to cart** button |
| CSS | Style | The button is blue |
| JavaScript | Behaviour | The count goes up on a click |

Think of a house: HTML is the walls, CSS is the paint, JavaScript is the electricity.

### Structure vs Style vs Behaviour

This box has a real page. Open the **index.html** tab to see the HTML and CSS. **main.js** adds the behaviour.

```js runnable preview="hello" title="Structure, style and behaviour in one small page"
const button = document.querySelector("#greet");
const message = document.querySelector("#message");
let items = 0;

button.textContent = "Add to cart";
message.textContent = "Your cart is empty.";

button.addEventListener("click", () => {
  items = items + 1;
  message.textContent = "Items in cart: " + items;
  console.log("Items in cart:", items);
});
```

Press the button in the **Preview**. The text changes each time, and the page never reloads.

### Why is JavaScript Important?

- Without it, a page can only show what the server sent.
- With it, the page reacts at once: errors while you type, a cart total that updates, more results as you scroll.

JavaScript and Java are different languages. Only the names are similar.

## First JavaScript Program

### Writing Hello World

```js runnable title="Your first JavaScript program"
console.log("Hello, World!");
```

Expected output:

```text
Hello, World!
```

Text inside quotes is a **string**. Change it to your name and run it again.

### Using `console.log()`

**Function** = a named piece of code that does a job when you call it.
`console.log()` is a function that prints. Separate several values with commas.

```js runnable title="One value, several values, and a calculation"
console.log("Asha");
console.log("Asha", "has", 3, "orders");
console.log("Total:", 250 + 120);
```

Expected output:

```text
Asha
Asha has 3 orders
Total: 370
```

### Understanding Output

- Lines from `console.log()` appear in the order they ran.
- If the **last line** is a value on its own, the box also shows it after `=>`.
- **Expression** = any code that gives a value, like `2 + 3` or `name.length`.

```js runnable title="A printed line, then the value of the last line"
const name = "Ravi";
console.log("Hello,", name);
name.length;
```

Expected output:

```text
Hello, Ravi
=> 4
```

The `=>` line is a feature of this site's boxes. A normal `.js` file prints only what `console.log()` prints.

## Client-Side vs Server-Side

### What is Client-Side JavaScript?

**Client** = the device that asks for the page, usually your browser.
Client-side code runs on your phone or computer, and can change the page through `document`.

### What is Server-Side JavaScript?

**Server** = the computer that sends the page.
Server-side code usually runs with **Node.js**. It has no page, but it can read files and talk to a database.

```js
// Server-side JavaScript with Node.js: reads a file on the server.
// It cannot run in a browser box, because a browser cannot read server files.
import { readFile } from "node:fs/promises";

const text = await readFile("orders.json", "utf8");
console.log(JSON.parse(text).length);
```

### Browser vs Server Execution

The language is the same everywhere. Only the extras differ.
Plain boxes on this site run in the background, with no page attached:

```js runnable title="Asking what this code box has"
console.log("console:", typeof console);
console.log("document:", typeof document);
console.log("window:", typeof window);
```

Expected output:

```text
console: object
document: undefined
window: undefined
```

`document` belongs to the browser page, not to the language. Boxes with a **Preview** tab have one.

### Real-Time Examples

| Task | Runs on | Why |
|---|---|---|
| "Card number too short" message | Client | The person must see it at once |
| Update the cart total | Client | No need to wait for the server |
| Check a password | Server | The real password must stay secret |
| Save an order | Server | Only the server talks to the database |

## Setting Up JavaScript

### Using JavaScript in HTML

A page runs JavaScript through a `<script>` element. The browser reads the page top to bottom and runs each script it reaches. There are two ways.

### Internal JavaScript

The code sits between `<script>` and `</script>` inside the HTML.
Put it at the end of `<body>`, so the elements already exist when it runs.

```html
<h1 id="title">Welcome</h1>
<script>
  document.querySelector("#title").textContent = "Welcome, Asha!";
</script>
```

### External JavaScript

The code sits in its own file, such as `app.js`. The page links to it with `src`.

```html
<h1 id="title">Welcome</h1>
<!-- Load the external file. The tag stays empty. -->
<script src="app.js"></script>
```

```js
// app.js
document.querySelector("#title").textContent = "Welcome, Asha!";
console.log("app.js has run");
```

- One file can be shared by many pages.
- The HTML stays tidy, and the browser can cache the file.
- This is what most real sites use.

## JavaScript on a Web Page

The preview boxes on this site work like an external file: **index.html** is the page, and **main.js** is loaded at the end of it.

```js runnable preview="hello" title="main.js changes the heading as soon as the page loads"
const title = document.querySelector("#title");
title.textContent = "Welcome, Asha!";
console.log("main.js changed the heading to:", title.textContent);
```

Open **index.html**. The heading there still says `Welcome to ShopEasy`. The Preview shows the new text, because main.js ran after the page loaded.

**Easy way to remember:** HTML = **what**, CSS = **how it looks**, JavaScript = **what it does**.

## TL;DR

- JavaScript is the behaviour of a web page.
- Code runs from top to bottom.
- `console.log()` prints; the last line's value shows after `=>` on this site.
- Browser code can change the page. Server code can use files and databases.
- Use `<script src="app.js"></script>` at the end of `<body>`.

## Try this

In the **Using `console.log()`** box, change the last line to `console.log("Total:", "250" + 120);`. Predict the output, then run it.

You get `Total: 250120`. With quotes, `"250"` is text, and `+` joins text. **Data Types** explains why.

## Check Yourself

## Common mistakes

| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| `Console.log("Hi")` | Capital letters do not matter | Names are case-sensitive | `Console is not defined` |
| Using `document` in a plain box | Every box has a page | Only Preview boxes have a page | `document is not defined` |
| `<script>` before the element | Order does not matter | Put scripts at the end of `<body>` | The script finds nothing |
| Code inside `<script src="app.js">` | Both run | A `src` script ignores its own code | The inner code never runs |
| `"250" + 120` | Quotes are only decoration | Quoted values are text | `250120` instead of `370` |

## Quick reference

<div className="tscroll">

| Syntax | Meaning |
|---|---|
| `console.log(value)` | print a value to the output |
| `console.log(a, b, c)` | print several values on one line, separated by spaces |
| `typeof value` | report the type of a value, such as `"number"` or `"string"` |
| `=> value` in the output | the value of the last line of a code box on this site |
| `<script> … </script>` | internal JavaScript, written inside the HTML page |
| `<script src="app.js"></script>` | external JavaScript, loaded from a separate file |
| client-side | runs in the browser, and can change the page |
| server-side | runs on the server, and can use files and databases |

</div>

## Exercises

## What to Remember

- HTML is the structure, CSS is the style, JavaScript is the behaviour.
- `console.log()` prints, and `=>` shows the last line's value here.
- The language is the same on the browser and the server.

Next: **Data Types**. Store values with `let` and `const`, and learn the kinds of values JavaScript has.
