Introduction

By Syncfusion®, Inc. · Last updated

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.

HTML what is there

CSS how it looks

JavaScript what it does

Cart: 0 → 1

Each language has one job. JavaScript is the one that reacts.

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.

Three instructions, run from top to bottom

const price = 25;
const discount = 5;
console.log("You pay:", price - discount);

Expected output:

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.

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:

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

LanguageJobShop example
HTMLStructureThe Add to cart button
CSSStyleThe button is blue
JavaScriptBehaviourThe 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.

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

Your first JavaScript program

console.log("Hello, World!");

Expected output:

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.

One value, several values, and a calculation

console.log("Asha");
console.log("Asha", "has", 3, "orders");
console.log("Total:", 250 + 120);

Expected output:

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.

A printed line, then the value of the last line

const name = "Ravi";
console.log("Hello,", name);
name.length;

Expected output:

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.

// 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:

Asking what this code box has

console.log("console:", typeof console);
console.log("document:", typeof document);
console.log("window:", typeof window);

Expected output:

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

TaskRuns onWhy
"Card number too short" messageClientThe person must see it at once
Update the cart totalClientNo need to wait for the server
Check a passwordServerThe real password must stay secret
Save an orderServerOnly 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.

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

<h1 id="title">Welcome</h1>
<!-- Load the external file. The tag stays empty. -->
<script src="app.js"></script>
// 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.

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

MistakeWrong ideaCorrect ideaWhat goes wrong
Console.log("Hi")Capital letters do not matterNames are case-sensitiveConsole is not defined
Using document in a plain boxEvery box has a pageOnly Preview boxes have a pagedocument is not defined
<script> before the elementOrder does not matterPut scripts at the end of <body>The script finds nothing
Code inside <script src="app.js">Both runA src script ignores its own codeThe inner code never runs
"250" + 120Quotes are only decorationQuoted values are text250120 instead of 370

Quick reference

SyntaxMeaning
console.log(value)print a value to the output
console.log(a, b, c)print several values on one line, separated by spaces
typeof valuereport the type of a value, such as "number" or "string"
=> value in the outputthe 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-sideruns in the browser, and can change the page
server-sideruns on the server, and can use files and databases

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.