Primitive Data Types: Numbers, Strings and Booleans
A student’s name is not the same kind of value as a mark
A school form records the student’s name, a maths mark and whether homework was submitted. The program should store these in forms that support the intended work: display a name, add a mark and make a yes-or-no decision.
A data type describes a kind of value and helps determine which operations make sense. Numbers represent quantities, strings represent text, and booleans represent true or false. The exact set of types and operation rules depends on the language.
Choose a type
Select suitable values for names, marks, student identifiers and yes-or-no information.
Explain similar-looking values
Distinguish the number 40 from the string “40,” and predict their behavior with addition.
Handle missing information
Explain why a missing mark differs from zero and identify important JavaScript type limits.
Choose a representation for each school field
Student name
Keep letters as text that a report can display.
"Maya" → stringMaths mark
Use a quantity that can take part in arithmetic.
40 → numberHomework status
Store whether the statement “homework submitted” is true.
false → boolean
Primitive does not mean unimportant or always small
A primitive value in JavaScript is a value that is not an object. Common primitives include numbers, strings and booleans. A long string is still a primitive; the word does not mean “one character” or “a tiny value.”
JavaScript primitives are immutable: an operation cannot change an existing primitive value in place. A variable declared with let can still be reassigned to a different value. Replacing the value associated with a name is different from changing the value itself.
The lesson on variables and assignment is the prerequisite. Here we choose the values that those names hold. More complex records and collections come in the next lesson.
Inspect three fields in a student record
This example uses JavaScript. Quotation marks create the string 'Maya'; the unquoted 40 is numeric; the unquoted keyword false is a boolean. typeof produces text describing a value’s JavaScript type.
Predict the three output lines. The name says what a field is for, while its stored value has a type. Naming a string maths would not turn it into a number.
JavaScript · local teaching example
const studentName = 'Maya';
const maths = 40;
const homeworkSubmitted = false;
console.log(typeof studentName);
console.log(typeof maths);
console.log(typeof homeworkSubmitted);Expected output
string
number
booleanDigits in quotes are still text
Both values look like forty when displayed, but this JavaScript example treats them differently. With two numbers, + adds. If one operand is a string, this use of + joins text; the other operand is converted to text.
The first result is 45. The second is the string 405, formed by joining “40” and “5.” This is why identifying a value’s type matters before arithmetic. Explicit conversion will help when marks arrive as text from a form.
JavaScript · local teaching example
const numericMark = 40;
const textMark = '40';
console.log(numericMark + 5);
console.log(textMark + 5);Expected output
45
405A student identifier can be text even if it contains only digits
A student identifier labels a record; it is not a quantity you need to add. Keeping '007' as text preserves its leading zeros. In this JavaScript example, Number creates the numeric value 7, losing the identifier’s original formatting.
Choose a type by the task rather than by appearance. Marks and attendance counts are quantities. Student names, phone numbers and formatted record identifiers are usually text in an application’s data model.
JavaScript · local teaching example
const studentId = '007';
console.log(studentId);
console.log(Number(studentId));Expected output
007
7False is a value, not an empty answer
A boolean has one of two values, true or false. In this JavaScript program, false records the answer “homework was not submitted.” It is different from not knowing whether homework was submitted.
The string 'false' is text, even though its letters spell a boolean keyword. Printing them can look similar, so the example also displays their types. Decisions using booleans are developed in conditional statements.
JavaScript · local teaching example
const submitted = false;
const submittedText = 'false';
console.log(submitted);
console.log(typeof submitted);
console.log(submittedText);
console.log(typeof submittedText);Expected output
false
boolean
false
stringDistinguish missing data from a zero mark
This JavaScript example declares science without an initial value, so it is undefined after the declaration. The explicit null value records that no previous school is listed. These are conventions a program must interpret consistently.
Neither missing value means a student earned zero. A genuine zero mark is a known numeric result and should remain distinguishable from an unentered result.
JavaScript’s typeof null reports 'object', a historical quirk. null is still a primitive. Do not rely on typeof alone to recognize it; the later comparison lesson explains direct value checks.
JavaScript · local teaching example
let science;
const previousSchool = null;
const maths = 0;
console.log(science);
console.log(previousSchool);
console.log(maths);
console.log(typeof previousSchool);Expected output
undefined
null
0
objectJavaScript has seven primitive types
JavaScript’s primitive types are string, number, boolean, undefined, null, bigint and symbol. We have used the first five. BigInt represents integers beyond the safe whole-number range of Number; it is not for fractional marks. Symbol produces unique values often used as property keys in more advanced programs.
You do not need BigInt or Symbol for the small school calculations in this course. Knowing that they exist prevents treating our three common types as JavaScript’s complete type system. See the JavaScript data types guide.
Languages organize types differently. JavaScript normally uses number for both 40 and 40.5; Python distinguishes integers (int) and floating-point numbers (float). Do not transfer one language’s type names and conversion rules to every language.
Choose values for three new school fields
The school adds an identifier that must retain its leading zero, an attendance count and a yes-or-no library permission. Use identifier 009, count 28 and permission granted. Choose their values and types before reading the answer.
Worked answer
Use the string '009' to preserve formatting, the number 28 for arithmetic and the boolean true for permission. The complete JavaScript example below prints the identifier followed by the three types.
JavaScript · local teaching example
const studentId = '009';
const attendanceCount = 28;
const canBorrowBooks = true;
console.log(studentId);
console.log(typeof studentId);
console.log(typeof attendanceCount);
console.log(typeof canBorrowBooks);Expected output
009
string
number
booleanA number type does not guarantee perfect decimal arithmetic
This JavaScript example adds two small decimal amounts. Ordinary Number values use binary floating-point representation, and some decimal fractions cannot be represented exactly. The printed result exposes a tiny representation difference.
For integer marks such as 40 and 35, this issue does not affect our total. For financial amounts or other values requiring exact decimal behavior, choose an appropriate representation and rounding policy rather than assuming every decimal operation is exact.
There is also a limit to exactly represented whole numbers: JavaScript Number has a safe integer range ending at Number.MAX_SAFE_INTEGER, 9,007,199,254,740,991. BigInt is an alternative for integer tasks beyond that range. See the Number documentation. A correct numeric type still does not establish that a school mark falls between 0 and 50.
JavaScript · local teaching example
console.log(0.1 + 0.2);Expected output
0.30000000000000004Check: choose a type by the task
Check: no result entered versus a real zero
Ask what the value means before choosing its type
Use numbers for quantities, strings for text and formatted identifiers, and booleans for true-or-false information. Missing data is different from zero or false. The language defines the available types and their operations.
In JavaScript, quoted digits remain strings, primitives cannot be changed in place, and typeof has a special case for null. Numeric representation also has precision and range limits.
Before moving on, explain why a school identifier can be text while an attendance count is numeric. Continue to organizing several values into collections and records, or review how names store and update values.
Reaching the recap and completing the quizzes are separate milestones.