We use cookies to enhance your experience on the site
CodeWorlds

Data Types in JavaScript

In Jurassic Park, every organism has specific characteristics - a T-Rex differs fundamentally from a Velociraptor, and both differ from a plant. JavaScript works similarly - every value has a type that determines what you can do with it.

Primitive Types

JavaScript has 7 primitive types - the basic "building blocks" of data:

String - Text

Strings represent text. You can use single quotes, double quotes, or backticks (template literals):

1const dinoName = 'Tyrannosaurus Rex';
2const species = "Velociraptor mongoliensis";
3const description = `The most dangerous predator in the park`;
4
5console.log(typeof dinoName); // "string"

Number - Numbers

JavaScript has one type for all numbers - integers and floats alike:

1const weight = 8000;        // integer (kg)
2const temperature = 37.5;   // float (°C)
3const age = -5;             // negative number
4
5console.log(typeof weight); // "number"
6
7// Special numeric values
8console.log(Infinity);      // infinity
9console.log(-Infinity);     // negative infinity
10console.log(NaN);           // Not a Number - result of invalid operations
11console.log(10 / 0);        // Infinity
12console.log('abc' * 2);     // NaN

Boolean - Logical Value

Only two values:

true
or
false
. Used in conditions:

1const isAlive = true;
2const isDangerous = false;
3const isCarnivore = true;
4
5console.log(typeof isAlive); // "boolean"
6
7// Boolean as result of comparisons
8console.log(8000 > 100);     // true
9console.log('Rex' === 'Blue'); // false

Null - Intentional Absence

null
means "intentionally no value" - the programmer explicitly set it to empty:

1let currentLocation = null; // dinosaur hasn't been located yet
2let lastMeal = null;        // feeding data not available
3
4console.log(typeof null); // "object" - this is a known JS quirk!
5console.log(currentLocation === null); // true

Undefined - Uninitialized Value

undefined
means "value was never assigned" - a variable was declared but never given a value:

1let nextFeedingTime;
2console.log(nextFeedingTime); // undefined
3console.log(typeof nextFeedingTime); // "undefined"
4
5// Function returns undefined if there's no return statement
6function checkDino() {
7  // no return
8}
9console.log(checkDino()); // undefined

Symbol - Unique Identifier

Symbols are always unique, even if they have the same description. Useful for unique identifiers:

1const id1 = Symbol('dinoId');
2const id2 = Symbol('dinoId');
3
4console.log(id1 === id2); // false - each Symbol is unique!
5console.log(typeof id1);  // "symbol"
6
7// Used as unique object keys
8const rex = {
9  [Symbol('id')]: 'DINO-001',
10  name: 'Rex'
11};

BigInt - Very Large Numbers

For integers larger than

Number.MAX_SAFE_INTEGER
(2^53 - 1):

1const dinosaurDNASequence = 9007199254740993n; // n at the end
2const bigNumber = BigInt(9007199254740993);
3
4console.log(typeof dinosaurDNASequence); // "bigint"

Complex Types

Object - Collections of Data

Objects store key-value pairs. They're used to represent more complex entities:

1const dinosaur = {
2  name: 'Rex',
3  species: 'Tyrannosaurus Rex',
4  weight: 8000,
5  isCarnivore: true,
6  location: null
7};
8
9console.log(typeof dinosaur); // "object"
10console.log(dinosaur.name);   // "Rex"

Array - Ordered List

Arrays are special objects for storing ordered collections:

1const zones = ['A', 'B', 'C', 'D'];
2const weights = [8000, 15, 6700, 25000];
3const mixed = ['Rex', 8000, true, null];
4
5console.log(typeof zones); // "object" - arrays are objects in JS!
6console.log(Array.isArray(zones)); // true - proper way to check for array

Function - Executable Code

Functions are also a type in JavaScript - they can be assigned to variables, passed as arguments:

1const roar = function() {
2  return 'ROOOAR!';
3};
4
5console.log(typeof roar); // "function"

Type Checking

The

typeof
operator returns a string with the type name:

1console.log(typeof 'Rex');       // "string"
2console.log(typeof 42);          // "number"
3console.log(typeof true);        // "boolean"
4console.log(typeof undefined);   // "undefined"
5console.log(typeof null);        // "object" (historical quirk!)
6console.log(typeof {});          // "object"
7console.log(typeof []);          // "object" (arrays are objects)
8console.log(typeof function(){}); // "function"

Type Conversion

JavaScript automatically converts types in some situations - this is called coercion:

1// Implicit (automatic) conversion
2console.log('5' + 3);    // "53" - number converted to string (concatenation)
3console.log('5' - 3);    // 2 - string converted to number
4console.log(true + 1);   // 2 - true is 1
5console.log(false + 1);  // 1 - false is 0
6console.log(null + 1);   // 1 - null is 0
7
8// Explicit conversion
9const str = '8000';
10const num = Number(str);   // 8000 (number)
11const bool = Boolean(str); // true (non-empty string is truthy)
12
13console.log(String(42));   // "42"
14console.log(Number('abc')); // NaN - can't convert
15console.log(Boolean(0));   // false
16console.log(Boolean(''));  // false
17console.log(Boolean(null)); // false
18console.log(Boolean([]));  // true (empty array is truthy!)

"Types in JavaScript are like dinosaur species classifications" - says Dr. Rex. "A T-Rex and a Velociraptor are both dinosaurs, but they have completely different properties and behaviors. Similarly, a string and a number are both JavaScript values, but you can do completely different things with them!"

Go to CodeWorlds