A program must remember values while it works: a name, a total, a mark. Variables store these values, and operators combine them.
Declaring Variables
A variable is a named place that holds a value. JavaScript has three keywords for declaring one. The keyword let declares a variable whose value may change later. The keyword const declares a variable that cannot be given a new value. The older keyword var still works but has confusing scope rules, so new code uses let and const. A good habit is to use const unless the value must change.
Definition: Variable. A named place that stores a value which a program can use and change.
A name may contain letters, digits, underscores and the dollar sign, but it cannot start with a digit. Names usually use camel case, for example totalMarks.
Data Types
Every value has a data type. The primitive types are number, string, boolean, null, undefined, symbol and bigint. A number holds integers and decimals together. A string is text in single or double quotation marks. A boolean is true or false. The value undefined means a variable has been declared but not given a value, and null means that there is no value on purpose. Arrays and objects are reference types that hold groups of values. The operator typeof reports the type of a value.
Definition: Data type. The kind of value stored, such as a number, a string or a boolean.
Table 3.2: Primitive data types in JavaScript
| Type | Example | Meaning |
|---|---|---|
| number | 42, 3.14 |
Integers and decimals |
| string | "hello" |
Text |
| boolean | true |
Yes or no value |
| undefined | let x; |
Declared, no value yet |
| null | null |
Empty on purpose |
Operators
An operator is a symbol that works on values. Arithmetic operators are +, -, *, /, % for the remainder and ** for powers. The comparison operators are <, >, <=, >=, == and ===. The operator == converts types before comparing, while === compares both type and value, so 5 == "5" is true and 5 === "5" is false. Strict comparison avoids surprises, so it is the standard choice. The logical operators are && for and, || for or, and ! for not.
Definition: Operator. A symbol, such as + or ===, that works on one or more values.
Example 3.2: Working with values
let price = 250;
const rate = 20;
let total = price + price * rate / 100;
console.log(total);
console.log(typeof total, typeof "250", typeof true);
console.log(7 % 3, 2 ** 5);
console.log(5 == "5", 5 === "5");
Output:
300
number string boolean
1 32
true false
Common mistakes
- Using == and being surprised that 5 equals the text "5".
- Changing a const variable, which causes an error.
- Starting a variable name with a digit.
- Expecting "5" + 3 to be 8, when it joins the text to give "53".
Key takeaways
- Use const for values that stay the same and let for values that change.
- The main types are number, string, boolean, null and undefined.
- Compare with === so that both type and value must match.
- The plus sign adds numbers but joins strings.
Practice questions
- What is the difference between let and const?
- What is the result of 7 % 3?
- What does typeof "hello" return?
Answers to the practice questions
- A let variable can be given a new value, but a const variable cannot.
- 1, the remainder of the division.
- The text string.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit III
