Lesson 2 / 11

Variables, Data Types and Template Literals

JavaScript has three ways to declare a variable, and picking the right one is a habit worth building early — use const by default, and let only when the value genuinely needs to change later.

const price = 19.99;   // won't be reassigned
let quantity = 3;      // will change
var oldStyle = "avoid"; // legacy syntax, avoid in new code

var predates let and const and has confusing scoping rules (it ignores block boundaries like if statements and loops) — modern JavaScript style guides and every major team avoid it entirely. You’ll still see it in older code and tutorials, so it’s worth recognizing, even though you shouldn’t write it yourself.

Core types

  • number — both integers and decimals, there’s no separate type for each like in Java or C++
  • string — text, in single quotes, double quotes, or backticks
  • booleantrue/false
  • undefined — a variable that’s been declared but never assigned a value
  • null — an intentional “no value”, explicitly assigned by your code

The difference between undefined and null confuses a lot of newcomers: undefined means “nobody set this yet,” while null means “someone deliberately set this to nothing.” JavaScript itself uses undefined automatically; your own code typically uses null on purpose.

Template literals

const name = "Yash";
const greeting = `Hello, ${name}! You have ${3 + 2} new messages.`;
console.log(greeting);

Backticks let you embed expressions directly in a string with ${...} — no more chaining + to build text piece by piece. Template literals also support multi-line strings without any special escape characters, which regular quoted strings don’t allow.

Checking a variable’s type

console.log(typeof 42);          // "number"
console.log(typeof "hello");     // "string"
console.log(typeof true);        // "boolean"
console.log(typeof undefined);   // "undefined"