⚡0 XP🔥0

Introduction, Variables and Data Types

Lesson 1 of 7 ⏱ 4 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs JavaScript

What JavaScript is and where it runs, console.log, let and const, the main data types, template strings, typeof and converting between strings and numbers.

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)
Did you know?

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.

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
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#

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
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.

Common Mistake

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"
▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
number string boolean
undefined
null
0.30000000000000004
Infinity -Infinity
NaN

Strings and template strings#

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
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.

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
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.

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
Neha will be 20 next year.

Summary#

  • Use const by default, let when the value changes; avoid var.
  • Types: number, string, boolean, undefined, null, object, bigint; check with typeof.
  • Template strings use backticks and ${...}.
  • Input is text - convert with Number() before maths.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Student card

Create const variables name = "Asha", course = "Web Development" and fees = 4999. Print exactly this line using a template string:

Asha joined Web Development. Fees: Rs 4999

Expected output:
Asha joined Web Development. Fees: Rs 4999
2

Add two numbers from input

prompt() returns text. Read two numbers (one per prompt() call), convert them with Number(), and print their sum and their product on two lines like this:

Sum: 19
Product: 84
Test input:
12
7
Expected output:
Sum: 19
Product: 84

Quick Quiz

Q1.

Which keyword should you use for a value that will never be re-assigned?

Q2.

What does typeof 42 return?

Q3.

What is "5" + 3 in JavaScript?

Q4.

Which line prints Hello, Ravi! when name = "Ravi"?

Q5.

What is the value of a variable declared with let x; but not assigned?