OSJavaScript.002: Values and Primitive Types — String, Number, Boolean, null, undefined, BigInt, Symbol, and typeof

Diverse students learning JavaScript values and primitive types around a laptop in a realistic colored-pencil classroom scene

Key Takeaways

  • JavaScript values have types, but variables are not permanently locked to one type.
  • JavaScript has seven primitive types: string, number, bigint, boolean, undefined, symbol, and null.
  • typeof is useful, but it has one famous historical surprise: typeof null returns "object".

In OSJavaScript.001, we established that JavaScript is the language, a JavaScript engine executes it, and a host environment such as a browser or Node.js supplies extra APIs. This lesson moves one level deeper: what kinds of values can JavaScript actually represent?

Unlike the Java lesson immediately before this one, where a variable declaration such as int age = 25; fixes the variable to an integer type, JavaScript is dynamically typed. A binding can hold a number now and a string later. That contrast is useful if you just read OSJava.002: Primitive Types and Variables.

LearnAwesome introduces JavaScript variables, data types, value-versus-reference ideas, and the typeof operator.

Values Have Types

A JavaScript program manipulates values. Every value belongs to a type. The official MDN data-types guide divides JavaScript values into primitives and objects. The primitive values are the simplest immutable values defined by the language.

const name = "Amina";
const count = 42;
const ready = true;

Here, "Amina" is a string value, 42 is a number value, and true is a boolean value. const prevents the binding from being reassigned; it does not create a special new data type.

JavaScript source code showing const declarations, numeric values, arrays, maps, loops, and object creation
Real JavaScript source code by Lionel Rowe via Wikimedia Commons, released under CC0 1.0.

The Seven Primitive Types

PrimitiveExampleTypical meaningtypeof
string"hello"Text"string"
number42, 3.14Ordinary numeric values"number"
bigint9007199254740993nArbitrarily large integers"bigint"
booleantrueLogical true/false state"boolean"
undefinedundefinedValue not assigned / absent by default"undefined"
symbolSymbol("id")Unique identifier-like primitive"symbol"
nullnullIntentional absence of a value/object reference"object" — historical quirk

Everything that is not a primitive value is an object. Arrays, ordinary objects, dates, maps, regular expressions, and functions all live on the object side of JavaScript’s type system, although typeof reports callable functions as "function".

String: Text Data

Strings represent text. JavaScript supports single quotes, double quotes, and template literals with backticks.

const first = "Bitcoin";
const second = 'JavaScript';
const message = `Learning ${second}`;

Strings are immutable: operations that appear to modify a string actually produce a new string value. If your editor visually separates strings, keywords, numbers, and identifiers, that is the same syntax highlighting covered in our earlier coding explainer.

Number: Integers and Decimals Share One Main Type

JavaScript’s ordinary number type uses IEEE 754 double-precision floating-point representation. That means ordinary integers and decimals normally share the same type.

typeof 42;      // "number"
typeof 3.14;    // "number"
typeof NaN;     // "number"
typeof Infinity;// "number"

NaN means “Not-a-Number,” but it is still a value in the Number type. That odd-looking detail makes more sense once you distinguish a value’s name from its underlying type.

BigInt: Integers Beyond Number’s Safe Integer Range

For integers larger than JavaScript can represent exactly with ordinary Number values, JavaScript provides BigInt. A BigInt literal ends with n.

const huge = 9007199254740993n;
console.log(typeof huge); // "bigint"

BigInt is not simply “a more precise Number.” It is a distinct primitive type for integers. You generally cannot mix BigInt and Number directly in arithmetic without an explicit conversion.

Boolean: true or false

A boolean has exactly two values: true and false. Booleans become especially important when we reach comparisons, conditionals, loops, and application state.

const connected = true;
const maintenanceMode = false;

undefined and null Are Not the Same

undefined often means JavaScript has no value there yet. For example, a let declaration without an initializer receives undefined.

let result;
console.log(result);        // undefined
console.log(typeof result); // "undefined"

null, by contrast, is normally written deliberately by the programmer to represent an intentional empty or missing value. The exact semantics are application-specific, but treating null and undefined as identical concepts is a common beginner mistake.

Desarrollo Útil walks through let, const, JavaScript primitive types, null, undefined, and typeof.

Symbol: Unique Primitive Values

A Symbol is a unique primitive value commonly used for unique property keys. Two symbols created with the same description are still different values.

const firstId = Symbol("id");
const secondId = Symbol("id");

console.log(firstId === secondId); // false

You will not need Symbol constantly in beginner programs, but it belongs in the complete primitive-type model and should not be omitted just because it is less common than strings or numbers.

typeof Lets You Inspect a Value’s Type

The unary typeof operator returns a string describing the operand’s type category. MDN’s typeof reference documents the exact return strings.

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

The Famous typeof null Surprise

console.log(typeof null); // "object"

This does not mean null is an object. null is a primitive value. The "object" result is a long-standing historical behavior preserved for web compatibility. To test specifically for null, use a strict comparison such as value === null.

A learnjavascript discussion tackles a classic beginner misconception: primitive values are not objects even though JavaScript can expose wrapper-provided methods on them.

Primitive Values Are Immutable

Primitive values themselves cannot be mutated. A variable can be reassigned to a different primitive, but that does not modify the old primitive value.

let word = "cat";
word = "dog";

The string "cat" was not changed into "dog". The binding named word was reassigned to a different string value. This distinction becomes important when you later compare primitives with objects and arrays.

JavaScript Is Dynamically Typed

A JavaScript binding does not permanently declare one value type. The value can change type at runtime.

let value = 42;
value = "forty-two";
value = true;

This flexibility is one reason TypeScript exists: it adds static type checking around JavaScript development. BitcoinVersus.Tech has followed that tooling layer in stories such as ts-rust Ports TypeScript 7’s Compiler to Rust and the earlier GitHub Copilot TypeScript Runtime rewrite.

A Small Type-Inspection Program

const values = [
  "hello",
  42,
  42n,
  true,
  undefined,
  null,
  Symbol("id")
];

for (const value of values) {
  console.log(value, typeof value);
}

Run this in the browser console introduced in OSJavaScript.001. Pay special attention to the line for null. The experiment is more useful than memorizing a list because it lets you observe the language directly.

Common Beginner Mistakes

  • Saying JavaScript has only six primitive types: modern JavaScript has seven; BigInt and Symbol count.
  • Calling null an object because typeof says object: null is a primitive.
  • Treating null and undefined as identical: they often represent different kinds of absence.
  • Assuming integers have a separate int type: ordinary integers and decimals normally use Number.
  • Mixing BigInt and Number arithmetic casually: they are distinct numeric types.
  • Thinking const makes an object immutable: const prevents rebinding; object mutation is a separate concept.

Practice

  1. Open a browser developer console.
  2. Create one value for each of JavaScript’s seven primitive types.
  3. Run typeof on each value.
  4. Confirm that typeof null returns "object".
  5. Compare two separately created symbols with the same description.
  6. Create a BigInt literal and inspect it with typeof.
  7. Declare a let binding, assign it a number, then a string, and observe that JavaScript permits the type to change.

Knowledge Check + Answers

  1. How many primitive types does JavaScript have? Seven.
  2. Name them. String, Number, BigInt, Boolean, Undefined, Symbol, and Null.
  3. Is null an object? No. It is a primitive value.
  4. What does typeof null return? "object", for historical compatibility reasons.
  5. What does typeof 42n return? "bigint".
  6. What is the difference between undefined and null at a beginner level? Undefined commonly represents a value that has not been supplied or assigned; null is commonly written deliberately to represent an intentional empty value.
  7. Can a let binding hold a number and later a string? Yes.
  8. Are primitive values mutable? No.
  9. What is Symbol useful for? Creating unique primitive values, often for unique object property keys.

Elementary Review

JavaScript values have types even though variables are dynamically typed. Learn the seven primitives first: string, number, bigint, boolean, undefined, symbol, and null. Use typeof to inspect values, but remember the special null result.

Next JavaScript Lesson

OSJavaScript.003: Variables and Constants — let, const, var, Reassignment, Scope, and the Temporal Dead Zone will build on these values by focusing on how JavaScript creates and manages bindings.

Editor’s Note

The featured artwork is a unique 1200×630 realistic color-pencil illustration created specifically for this lesson and is not reused in the body. The separate body image is real JavaScript source code by Lionel Rowe from Wikimedia Commons, released under CC0 1.0.

BitcoinVersus.Tech content is provided for informational and educational purposes.

One response to “OSJavaScript.002: Values and Primitive Types — String, Number, Boolean, null, undefined, BigInt, Symbol, and typeof”

  1. […] All three assignments are valid. This differs from the static variable declarations introduced in OSJava.002, while it is conceptually closer to the dynamic binding behavior shown in OSJavaScript.002. […]

    Like

Leave a Reply