3  Data Types

JavaScript has several built-in data types that form the foundation of how you’ll work with data in your programs. Since you have experience with R and Python, I’ll make some comparisons where helpful.

3.1 Primitive Data Types

JavaScript has 7 primitive data types. These are immutable (cannot be changed) and are passed by value:

3.1.1 Number

JavaScript uses a single number type for both integers and floating-point values.

// Both integers and decimals use the same type
let integer = 42;
let decimal = 3.14;
let scientific = 2.998e8; // Scientific notation

Unlike R (which has numeric) or Python (which distinguishes between int and float), JavaScript simplifies all numbers into a single type based on the IEEE 754 standard (64-bit floating-point).

3.1.2 String

Text data, surrounded by single quotes, double quotes, or backticks:

let singleQuoted = 'Hello';
let doubleQuoted = "World";
let templateLiteral = `Hello ${singleQuoted}`; // Template literals (with backticks) allow variable interpolation

The backtick syntax (template literals) is particularly useful as it’s similar to f-strings in Python or glue in R.

3.1.3 Boolean

Represents logical values:

let isTrue = true;
let isFalse = false;

3.1.4 Undefined

Represents a variable that has been declared but not assigned a value:

let undefinedVariable;
console.log(undefinedVariable); // Outputs: undefined
undefined

This is somewhat similar to NULL in R or None in Python, but with an important distinction…

3.1.5 Null

Represents the intentional absence of any value:

let emptyValue = null;

The difference between undefined and null often confuses beginners. Think of undefined as “no value yet” and null as “intentionally empty.”

3.1.6 Symbol

A newer type introduced in ES6 (2015) for creating unique identifiers:

let uniqueId = Symbol('description');

You likely won’t use these often as a beginner, but they’re important for more advanced applications.

3.1.7 BigInt

Another newer type for working with integers larger than the Number type can handle:

let hugeNumber = 9007199254740991n; // The 'n' suffix indicates a BigInt

3.2 Reference Data Types

These are mutable (can be changed) and are passed by reference:

3.2.1 Object

The foundation of JavaScript, similar to dictionaries in Python or lists in R:

let person = {
    name: 'John',
    age: 30,
    isStudent: false
};

// Accessing properties
console.log(person.name);       // Using dot notation
console.log(person['name']);    // Using bracket notation (like Python dictionaries)
John
John
John

3.2.2 Array

A special type of object for storing ordered collections:

let fruits = ['apple', 'banana', 'orange'];

// Accessing elements (zero-indexed, like in Python)
console.log(fruits[0]); // Outputs: 'apple'
apple
const arr = new Array(5); // Creates an array of length 5
console.log(arr); 
[ <5 empty items> ]
const arr2 = Array.from('hello'); // Creates an array from a string
console.log(arr2); 
[ 'h', 'e', 'l', 'l', 'o' ]
// Grabbing the last element of an array
arr2[arr2.length - 1]
'o'
// Array destructuring
const [x, y] = [1, 2];
console.log(x, y);
1 2
const [a, b, ...rest] = [1, 2, 3, 4, 5]; // 'rest' will be an array of the remaining elements
console.log(a, b, rest); // Outputs: 1 2 [3, 4, 5]
1 2 [ 3, 4, 5 ]
// Copying arrays using spread operator

const x = [1, 2, 3, 4]
const y = [...x]

y.push("hello")

console.log(x, y)
[ 1, 2, 3, 4 ] [ 1, 2, 3, 4, 'hello' ]

Unlike R’s vectors which are 1-indexed, JavaScript arrays are 0-indexed like Python lists.

3.2.3 Function

Functions are actually objects in JavaScript:

function greet(name) {
    return `Hello, ${name}!`;
}

// Functions can be assigned to variables
let sayHello = greet;
console.log(sayHello('John')); // Outputs: 'Hello, John!'
Hello, John!

This is similar to how you can assign functions to variables in R and Python, but JavaScript treats functions as first-class objects more thoroughly.