Data Types

Understanding Primitive Types

Emily Y Leung
3 min readAug 27, 2023

Field notes while going through the JavaScript Roadmap

Photo by Luca Bravo on Unsplash

What are Data Types?

  • Every variable in JavaScript has a data type which is used to describe the operations possible on that value
  • There are 7 primitive data types: Number, BigInt, String, Boolean, Null, Undefined and Symbol
  • The Object data type is not considered a primitive — and will be covered in a separate post

String

A string is a sequence of textual characters

Each character in a string has a given position, formally known as an index. To access a character in a string, we specify its index as an integer (starting from 0)

let lang = "JavaScript";

let firstLetter = lang[0];

console.log(firstLetter); // Output: "J"

Number

A Number is an integer or whole number without a decimal point

let value = 123;

A float or floating-point number is a Number with a decimal point

let value = -1.23;

Boolean

A Boolean is logical data type that can only return a true or false value

let isAdmin = true;

Undefined

Undefined is a data type which describes the absence of a value. This usually happens when a variable is declared but never initialized (i.e. no value was assigned)

let age;

console.log(age); // Output: undefined

BigInt

A BigInt is a data type used to store integer values that are too big to be represented by a Number

let totalSum = 9999999999;

Null

A Null data type is used to indicate the absence of an object. It is a data type which can only be initialized by a human — never the result of a function.

let num = null;

console.log(num); // Output: null

Symbol

A Symbol is a unique and immutable value (cannot be changed) which is used to create unique property keys so that they don’t clash with other keys.

const value1 = Symbol("hello");
const value2 = Symbol("hello");

console.log(value1 === value2); // Output: false

While value1 and value2 both have the same content, they are considered different values.

Symbols can also be stored as a key in an object using square brackets

let id = Symbol("id");

let person = {
name: "Jack",
[id]: 123
}

console.log(person); // Output: { name: "Jack", Symbol(id): 123 }

Symbols are not included in for loops

let id = Symbol("id");

let person = {
name: "Jack",
age: 25,
[id]: 12
}

for (let key in person) {
console.log(key);
}

// Output:
// name
// age

What is the benefit of using Symbols in Object?

It can be used as a key so that it cannot be accessed or changed by another program.

Example:

let person = {
name: "Jack"
}

// first program adds an id to a person
person.id = 12;
console.log(person.id) // Output: 12

// second program overwrites value
person.id = 42;
console.log(person.id) // Output: 42

If we instead assign the id as a Symbol , the original value does not get overwritten

let person = {
name: "Jack"
}

// first program adds an id to a person
let id = Symbol("id");
person[id] = 12;

console.log(person.id); // Output: undefined
console.log(person[id]); // Output: 12

// second program adds own key
person.id = 42

console.log(person.id); // Output: 42

I personally have yet to use Symbol in my own code, but I think I understand it’s potential.

Symbol methods:

  • for() — searches for existing symbols
  • keyFor() — returns a shared symbol key from the global symbol registry
  • toSource() — returns a string containing the source of the Symbol object
  • toString() — returns a string containing the description of the Symbol
  • valueOf() — returns the primitive value of the Symbol object
// get symbol by name
let sym = Symbol.for('id');

console.log(Symbol.keyFor(sym)); // Output: "id"

--

--

Emily Y Leung
Emily Y Leung

Written by Emily Y Leung

Creative. Problem solver. Learning programming in public | emilyyleung.github.io

No responses yet