What is JavaScript?#
JavaScript (JS) is the programming language of the web. HTML gives a page structure, CSS styles it, and JavaScript makes it do things - show a menu, check a form, load new posts without reloading, run a game.
JavaScript runs:
- in every browser (Chrome, Edge, Firefox, Safari) - no installation needed
- on servers with Node.js (Netflix, PayPal, LinkedIn use it)
- in mobile and desktop apps (React Native, Electron - VS Code is built with JS)
Java and JavaScript are completely different languages. The similar name was a marketing decision in 1995!
Your first program#
console.log() prints a line - in the browser's Console (F12 → Console) and in the output box here.
Namaste from JavaScript! 14 Total marks: 180
Each statement can end with a semicolon ;. They are optional in most cases, but using them consistently avoids surprises. Comments start with // (one line) or /* ... */ (many lines).
Variables: let and const#
Vindhyaedu has 135 students
| Keyword | Re-assign? | Use |
|---|---|---|
const |
no | default choice - most values never change |
let |
yes | counters, totals, values that change |
var |
yes | old style - avoid (confusing scope rules) |
Rules for names: letters, digits, _ and $; can't start with a digit; case-sensitive (marks ≠ Marks). The JS style is camelCase: totalMarks, studentName.
Re-assigning a const is an error: const pi = 3.14; pi = 3; → TypeError: Assignment to constant variable.
Variable एक डिब्बा है जिसमें value रखते हैं। const वाला डिब्बा एक बार भरने के बाद बदला नहीं जा सकता, let वाले डिब्बे में बाद में नई value रख सकते हैं। आदत डालें: पहले const लिखें, ज़रूरत पड़े तभी let।
Data types#
| Type | Example | typeof |
|---|---|---|
| number | 42, 3.14, -7 |
"number" |
| string | "Indore", 'hi', `hello` |
"string" |
| boolean | true, false |
"boolean" |
| undefined | a variable with no value | "undefined" |
| null | "intentionally empty" | "object" (a famous old bug) |
| object / array | { name: "Asha" }, [1, 2, 3] |
"object" |
| bigint | 12345678901234567890n |
"bigint" |
number string boolean undefined null 0.30000000000000004 Infinity -Infinity NaN
Strings and template strings#
Hello, Ravi! Hello, Ravi! You scored 87. Next year: 92 is the target 4 RAVI R INDORE
Converting types#
Values typed by users (from prompt() or a form input) are always strings. Convert them before doing maths.
105 15 42 3.5 991 7.46 NaN
Reading input with prompt()#
In a browser, prompt("question") shows a small box. In this course's editor, prompt() reads the next line from the Input box.
Neha will be 20 next year.
Summary#
- Use
constby default,letwhen the value changes; avoidvar. - Types: number, string, boolean, undefined, null, object, bigint; check with
typeof. - Template strings use backticks and
${...}. - Input is text - convert with
Number()before maths.