PedroGeoGISdev
  • Home
  • Linux OS
    • Linux: concepts
    • Linux: basic concepts
    • Linux: Bash
    • Linux: su and sudo
    • Linux: pipes
    • Linux: File System
    • Linux: Virtual Machines

    • Linux: distros
    • Linux Distros: Ubuntu
    • Linux Distros: Mint
    • Linux Distros: Debian
    • Linux Distros: openSuse
    • Linux Distros: Manjaro
    • Linux Distros: Red Hat Enterprise

    • Linux: laboratories
    • Linux Lab#LI01-1: Choose Linux
    • Linux Lab#LI01-2: Install at least three distributions
    • Linux Lab#LI01-3: Adjust user permissions
    • Linux Lab#LI02-1: Export env user with grep and pipe
    • Linux Lab#LI03-1: Manage users and groups
    • Linux Lab#LI03-2: Manage files
    • Linux Lab#LI03-3: Manage software
    • Linux Lab#LI03-4: Manage hardware
    • Linux Lab#LI04-1: Bash scripting, qtool
    • Linux Lab#LI04-2: Bash scripts as terminal tool
    • Linux Lab#LI04-3: Distribute the terminal app

    • Linux readings
    • Linux Resources
  • DevOps
    • What is DevOps
    • DevOps: Introduction
    • DevOps: Agile and Microservices
    • Infrastructure as code (IaC)
    • Immutable Infrastructure
    • Software Lifecycle

    • Documentation
    • How to document: Quarto and Obsidian

    • Network protocols
    • Network: Basics
    • Network: Client-server
    • Network Protocols
    • Network: DNS
    • Network: API Rest
    • Network: gRPC
    • Network: Websocket
    • Network: SMTP
    • Network: Ping
    • Network: UDP
    • Network: webhook
    • Network: SOAP
    • Network: graphQL

    • Version Control
    • Git
    • GitHub
    • Idea and GitHub 2023
    • Git and GitHub 2023 CLI

    • IDEs
    • IDE: Visual Code
    • IDE: IntellJIdea

    • DevOps tools
    • Amazon Web Services AWS
    • Docker
    • Jenkins pipelines
    • Kubernetes k8s
    • Digital Ocean
    • Nagios
    • Ansible

    • DevOps Laboratories
    • Lab 1: chat App
    • Lab 2: Spring Boot AWS AEB manually
    • Lab 3: Spring Boot and AWS S3 publisher
    • Lab 4: Spring Boot Docker/Jenkins
    • Lab 5: k8s on Digital Ocean
    • Lab 6: Spring Boot AWS codecommit

    • DevOps readings
    • DevOps Resources
  • MarkUp
    • MarkUp Languages
    • Introduction Markup
    • HTML Markup
    • Markdown Markup
    • Markdown and HTML working together, good idea?

    • Quarto Markdown
    • Quarto Markdown: basics
    • Quarto Markdown: creating
    • Quarto Markdown: publishing
    • Quarto Markdown: code & data
    • Quarto Markdown: api rest call
    • Quarto Markdown: OJS Cells
    • Quarto Markdown: cheat-sheet

    • Styling: CSS
    • Cascade Style Sheet
    • Cascade Style Sheet: Box Model and Containers
    • CSS: W3.css

    • MarkUp Languages Laboratories
    • Lab#MD01-1: Create and publish by Quarto

    • MarkUp Languages readings
    • MarkUp Languages Resources
  • Java SE
    • What is Java SE
    • Java Standard Edition: Basics
    • Java Standard Edition: Principles
    • Java MOOC Helsinki
    • Java MOOC Helsinki Syllabus

    • Java Create Project
    • Java SE: Maven
    • Java SE: Create Maven Project
    • Java SE: Project push GitHub
    • Java SE: JUnit and TDD

    • Java Concepts
    • Java SE: Class and Objects
    • Java SE: Scope
    • Java SE: static modifier
    • Java SE: Coupling and DDD
    • Java SE: Packages
    • Java SE: Abstract/Interface
    • Java SE: Java 8

    • Java Principles
    • Java SE: Encapsulation
    • Java SE: Abstraction
    • Java SE: Inherence
    • Java SE: Polymorphism

    • Java Design Patterns
    • Java Patterns: UML
    • Java Patterns: Types
    • Singleton
    • Factory
    • Abstract Factory
    • Builder
    • Facade
    • Bridge
    • Decorator
    • Composite
    • Observer
    • Strategy
    • State
    • Commander

    • Java SE Laboratories
    • Lab#SE00-1: Maven Person
    • Lab#SE00-2: Maven Clinic
    • Lab#SE00-3: Library Model
    • Lab#SE00-4: Abstract/Interface Human
    • Lab#SE01-1: Maven/Gradle Person and Account
    • Lab#SE01-2: Maven/Gradle Person and Account stored in JSON
    • Lab#SE02-1: Movie/Review, Model
    • Lab#SE02-2: Movie/Review, CRUD Operations
    • Lab#SE02-3: Movie/Review, factory
    • Lab#SE02-4: Movie/Review, interactivity and coupling
    • Lab#SE02-5: Movie/Review, simulate interactivity by console
    • Lab#SE03-1: Library/Book, Core-Model
    • Lab#SE03-2: Library/Book, Sprint Zero
    • Lab#SE03-3: Library/Book, Expand Model
    • Lab#SE04-1: healthyFood Restaurant, Core Model

    • Java SE readings
    • Java SE Resources
  • Python
    • Python Basics
    • Python: Basic Concepts
    • Python: Numbers
    • Python: Strings
    • Python: Conditionals
    • Python: Lists and Loops
    • Python: Nested Loops
    • Python: Tuples
    • Python: Sets
    • Python: Dictionaries
    • Python: Functions
    • Python: Higher-order Functions
    • Python: Variable Scope
    • Python: Variable Assigment
    • Python: None keyword in Python
    • Python: Object Oriented Programming (OOP)
    • Python: Inheritance and Polymorphism
    • Python: Encapsulation
    • Python: Abstraction
    • Python: Cheat-Sheets
  • JavaScript
    • JavaScript Basics
    • JavaScript: Fundamentals
    • JavaScript: Data Types
    • JavaScript: Variables
    • JavaScript: Operators
    • JavaScript: Conditionals
    • JavaScript: Loops
    • JavaScript: Functions
    • JavaScript: Arrays
    • JavaScript: Objects

    • React Framework

    • ReactJS: Principles
    • React JS: Introduction
    • React JS: render virtual DOM
    • React JS: Create a React project
    • React JS: Components
    • React JS: JSX
    • React JS: props and state

    • JavaScript: web scripting
    • JavaScript: basics
    • JavaScript: functions
    • JavaScript: objects
    • JavaScript: variables
    • JavaScript: flux control

    • ES6: ECMAScript 6
    • React JS ES6: arrow functions
    • React JS ES6: import modules
    • React JS ES6: array, data and key
    • React JS ES6: destructuring
    • React JS ES6: spread operator

    • ReacJS 18: Hooks
    • React JS: Rules of Hooks
    • ReactJS: useState
    • React JS: useReducer
    • React JS: useRef
    • React JS: useEffect
    • React JS: useContext
    • ReactJS: useMemo
    • ReactJS: custom hooks

    • ReactJS: Designing an App
    • React JS App: async
    • React JS App: events
    • React JS App: router
    • React JS App: conditional render
    • React JS App: styling

    • React JS: Laboratories
    • Lab#RE01-1: API Rest Axios
    • Lab#RE02-1: Router & Hooks
    • Lab#RE03-1: to-do app
    • Lab#RE03-2: HighCharts
    • Lab#RE03-3: API Rest Mono
    • Lab#RE03-4: API Rest Domains
    • Lab#RE03-5: data management
    • Lab#RE04-1: todo & server
    • Lab#RE04-2: Spring Boot & ReactJS
    • Lab#RE05-1: chat & websockets
    • Lab#RE05-2: chat: backend
    • Lab#RE05-3: chat & AWS
    • Lab#RE05-4: chat: test ws AWS
    • Lab#RE05-5: chat & front
    • Lab#RE05-6: chat & ws: front
    • Lab#RE06-1: healthyFood Restaurant
    • Lab#RE06-1-PR: create a pull request
    • Lab#RE07-1: traffic lights simulation

    • React JS readings
    • ReactJS Resources
  • Backend
    • Spring Legacy
    • Spring Framework
    • Spring MVC
    • Springs Servlets

    • Spring Boot Basics
    • Spring Boot: fundamentals
    • Spring Boot: create a Project
    • Spring Boot: H2 DB and Thymeleaf
    • Spring Boot: cycle

    • Spring Boot Concepts
    • Spring Boot: Dependency Injection
    • Spring Boot: Annotations
    • Spring Boot: Controller
    • Spring Boot: View
    • Spring Boot: Thymeleaf
    • Spring Boot: Vaadin Flow
    • Spring Boot: Vaadin Hilla
    • Spring Boot: Model
    • Spring Boot: Rest
    • Spring Boot: Data & DB
    • Spring Boot: JPA & DI
    • Spring Boot: JPA Mappings
    • Spring Boot: JPA Relationships
    • Spring Boot: JPA Queries
    • Spring Boot: JPA Inherence
    • Spring Boot: Scaling

    • Spring Boot Laboratories
    • Lab#SB00-1: Library UML
    • Lab#SB00-2: CRUD User
    • Lab#SB00-3: LibraryManagement
    • Lab#SB00-4: API Rest
    • Lab#SB00-5: Rest & JPA-H2
    • Lab#SB00-6: Rest & MongoDB
    • Lab#SB00-7: Styling
    • Lab#SB01-1: DataBase
    • Lab#SB02-1: JPA Relationships
    • Lab#SB03-1: APIs & cloud
    • Lab#SB04-1: JPA Inherence
    • Lab#SB05-1: API Rest
    • Lab#SB06-1: employeeCourse
    • Lab#SB07-1: monitor Book
    • Lab#SB08-1: Restaurant UML
    • Lab#SB08-2: Vaadin
    • Lab#SB08-3: H2 and API Rest
    • Lab#SB08-4: JPA
    • Lab#SB08-5: Test API Rest
    • Lab#SB09-1: SpringIO Conference

    • Spring Boot readings
    • Spring Boot Resources
  • Learning
    • Vocabulary
    • General Vocabulary
    • SCRUM Vocabulary
    • DevOps Vocabulary
    • Java SE Vocabulay
    • Spring Boot Vocabulary
    • DataBase Vocabulary
    • ReactJS Vocabulary
    • Web Vocabulary

    • Learning
    • Useful Questions
    • Learning: tips
    • Writing
    • Taking Notes
    • Comments
    • Document
    • Auto-Evaluate

    • Books & Articles
    • Books
    • Articles

    • What is SCRUM
    • SCRUM Agile Methodology
    • Agile Manifesto & Values
    • SCRUM Guide

    • Scrum Steps
    • Meetings, Impediments and Iterations
    • User stories, Tasks and Habits
    • Delivering Value & Communication
    • ScrumMaster, how it works
    • Mindset, the key to everything
    • Product Owner, how it works
    • Managing Time & Mind
    • Team & the Specialist
    • Albertus’ Dilemma
    • Before SCRUM
    • Team Dynamics
    • Emotions and Thoughts
    • Decision Making and Intuition
    • Beyond SCRUM
    • Balances, atmosphere and tools

    • Resources
    • SCRUM Resources
  • QGIS
    • QGIS basics
    • QGIS: basic concepts

    • QGIS laboratories
    • QGIS Laboratory 1: Introduction to Open Source GIS
  • ArcGIS Pro
    • ArcGIS Pro basics
    • ArcGIS Pro: basic concepts

    • ArcGIS Pro laboratories
    • ArcGIS Pro Laboratory 1: Getting Started
  • Bookmarks
    • Online Resources
    • Online Resources
  • About
    • About me and this site
    • About me
    • About this site
    • About images credit
  • Maps
  • Featured Projects
    • ✈️ Flight tracker
    • 🧑 People tracker
    • 🚗 Renting Car
    • Email
    • GitHub
    • LinkedIn
  1. JavaScript Basics
  2. JavaScript: Data Types
  • JavaScript

  • JavaScript Basics
    • JavaScript: Fundamentals
    • JavaScript: Data Types
    • JavaScript: Variables
    • JavaScript: Operators
    • JavaScript: Conditionals
    • JavaScript: Loops
    • JavaScript: Functions
    • JavaScript: Arrays
    • JavaScript: Objects

  • React Framework

  • ReactJS: Principles
    • React JS: Introduction
    • React JS: render virtual DOM
    • React JS: Create a React project
    • React JS: Components
    • React JS: JSX
    • React JS: props and state

  • JavaScript: web scripting
    • JavaScript: basics
    • JavaScript: functions
    • JavaScript: objects
    • JavaScript: variables
    • JavaScript: flux control

  • ES6: ECMAScript 6
    • React JS ES6: arrow functions
    • React JS ES6: import modules
    • React JS ES6: array, data and key
    • React JS ES6: destructuring
    • React JS ES6: spread operator

  • ReacJS 18: Hooks
    • React JS: Rules of Hooks
    • ReactJS: useState
    • React JS: useReducer
    • React JS: useRef
    • React JS: useEffect
    • React JS: useContext
    • ReactJS: useMemo
    • ReactJS: custom hooks

  • ReactJS: Designing an App
    • React JS App: async
    • React JS App: events
    • React JS App: router
    • React JS App: conditional render
    • React JS App: styling

  • React JS: Laboratories
    • Lab#RE01-1: API Rest Axios

    • Lab#RE02-1: Router & Hooks

    • Lab#RE03-1: to-do app
    • Lab#RE03-2: HighCharts
    • Lab#RE03-3: API Rest Mono
    • Lab#RE03-4: API Rest Domains
    • Lab#RE03-5: data management

    • Lab#RE04-1: todo & server
    • Lab#RE04-2: Spring Boot & ReactJS

    • Lab#RE05-1: chat & websockets
    • Lab#RE05-2: chat: backend
    • Lab#RE05-3: chat & AWS
    • Lab#RE05-4: chat: test ws AWS
    • Lab#RE05-5: chat & front
    • Lab#RE05-6: chat & ws: front

    • Lab#RE06-1: healthyFood Restaurant
    • Lab#RE06-1-PR: create a pull request

    • Lab#RE07-1: traffic lights simulation

  • React JS readings
    • ReactJS Resources

On this page

  • 1 Overview
  • 2 Dynamic Typing
  • 3 Number
    • 3.1 Special Numeric Values
  • 4 BigInt
    • 4.1 BigInt Operations
  • 5 String
    • 5.1 Three Types of Quotes
    • 5.2 Template Literals (Backticks)
    • 5.3 No Character Type
  • 6 Boolean (Logical Type)
    • 6.1 Boolean from Comparisons
  • 7 The “null” Value
  • 8 The “undefined” Value
    • 8.1 Explicitly Assigning undefined
  • 9 Objects and Symbols
    • 9.1 Objects
    • 9.2 Symbols
  • 10 The typeof Operator
    • 10.1 typeof Quirks
    • 10.2 typeof Syntax
  • 11 Type Comparison Table
  • 12 Examples
    • 12.1 Example 1: Type Checking
    • 12.2 Example 2: Working with Different Types
    • 12.3 Example 3: null vs undefined
    • 12.4 Example 4: Template Literals
    • 12.5 Example 5: Special Values
  • 13 Summary
  • Edit this page
  • View source
  • Report an issue
  1. JavaScript Basics
  2. JavaScript: Data Types

JavaScript: Data Types

JavaScript Data Types

javascript
data-types
primitives
JavaScript Data Types - Primitive and Reference Types
Author

Pedro GeoGIS

Published

Monday, January 13, 2025

Modified

Monday, October 13, 2025

1 Overview

📘 Data Types

Data types in JavaScript define the kind of values that can be stored and manipulated in a program. JavaScript has eight basic data types: seven primitive types (Number, BigInt, String, Boolean, null, undefined, Symbol) and one reference type (Object). JavaScript is dynamically typed, meaning variables can hold values of any type and can be reassigned to different types during execution.


2 Dynamic Typing

A value in JavaScript is always of a certain type. For example, a string or a number.

We can put any type in a variable. A variable can at one moment be a string and then store a number:

// no error
let message = "hello";
message = 123456;

Programming languages that allow such things are called “dynamically typed”, meaning that data types exist, but variables are not bound to any of them.


3 Number

let n = 123;
n = 12.345;

The number type represents both integer and floating point numbers.

There are many operations for numbers: - Multiplication: * - Division: / - Addition: + - Subtraction: - - And more


3.1 Special Numeric Values

Besides regular numbers, there are “special numeric values” which also belong to this data type: Infinity, -Infinity, and NaN.


3.1.1 Infinity

Infinity represents the mathematical Infinity ∞. It is a special value that’s greater than any number.

We can get it as a result of division by zero:

alert(1 / 0); // Infinity

Or just reference it directly:

alert(Infinity); // Infinity

3.1.2 NaN (Not a Number)

NaN represents a computational error. It is a result of an incorrect or undefined mathematical operation:

alert("not a number" / 2); // NaN, such division is erroneous

NaN is sticky. Any further mathematical operation on NaN returns NaN:

alert(NaN + 1);  // NaN
alert(3 * NaN);  // NaN
alert("not a number" / 2 - 1); // NaN

So, if there’s a NaN somewhere in a mathematical expression, it propagates to the whole result (exception: NaN ** 0 is 1).


Note

Math is “safe” in JavaScript

We can do anything: divide by zero, treat non-numeric strings as numbers, etc. The script will never stop with a fatal error. At worst, we’ll get NaN as the result.


4 BigInt

In JavaScript, the number type cannot safely represent integer values larger than (2^53-1) (that’s 9007199254740991), or less than -(2^53-1) for negatives.

For most purposes, the ±(2^53-1) range is quite enough, but sometimes we need really big integers, e.g., for cryptography or microsecond-precision timestamps.

BigInt type was recently added to represent integers of arbitrary length.

A BigInt value is created by appending n to the end of an integer:

// the "n" at the end means it's a BigInt
const bigInt = 1234567890123456789012345678901234567890n;

4.1 BigInt Operations

const bigNum1 = 9007199254740991n;
const bigNum2 = 9007199254740991n;

console.log(bigNum1 + bigNum2); // 18014398509481982n
console.log(bigNum1 * 2n);      // 18014398509481982n

5 String

A string in JavaScript must be surrounded by quotes.

let str = "Hello";
let str2 = 'Single quotes are ok too';
let phrase = `can embed another ${str}`;

5.1 Three Types of Quotes

In JavaScript, there are 3 types of quotes:

  1. Double quotes: "Hello"
  2. Single quotes: 'Hello'
  3. Backticks: `Hello`

Double and single quotes are “simple” quotes. There’s practically no difference between them in JavaScript.


5.2 Template Literals (Backticks)

Backticks are “extended functionality” quotes. They allow us to embed variables and expressions into a string by wrapping them in ${…}:

let name = "John";

// embed a variable
alert(`Hello, ${name}!`); // Hello, John!

// embed an expression
alert(`the result is ${1 + 2}`); // the result is 3

The expression inside ${…} is evaluated and the result becomes a part of the string.


Warning

This can only be done in backticks. Other quotes don’t have this embedding functionality!

alert("the result is ${1 + 2}"); // the result is ${1 + 2} (double quotes do nothing)

5.3 No Character Type

In some languages (C, Java), there is a special “character” type for a single character called char.

In JavaScript, there is no such type. There’s only one type: string. A string may consist of zero characters (be empty), one character, or many of them.


6 Boolean (Logical Type)

The boolean type has only two values: true and false.

This type is commonly used to store yes/no values: - true means “yes, correct” - false means “no, incorrect”

let nameFieldChecked = true;  // yes, name field is checked
let ageFieldChecked = false;  // no, age field is not checked

6.1 Boolean from Comparisons

Boolean values also come as a result of comparisons:

let isGreater = 4 > 1;
alert(isGreater); // true (the comparison result is "yes")

7 The “null” Value

The special null value does not belong to any of the types described above.

It forms a separate type of its own which contains only the null value:

let age = null;

In JavaScript, null is not a “reference to a non-existing object” or a “null pointer” like in some other languages.

It’s just a special value which represents: - “nothing” - “empty” - “value unknown”

The code above states that age is unknown.


8 The “undefined” Value

The special value undefined also stands apart. It makes a type of its own, just like null.

The meaning of undefined is “value is not assigned”.

If a variable is declared but not assigned, then its value is undefined:

let age;
alert(age); // shows "undefined"

8.1 Explicitly Assigning undefined

Technically, it is possible to explicitly assign undefined to a variable:

let age = 100;

// change the value to undefined
age = undefined;

alert(age); // "undefined"

But we don’t recommend doing that. Normally, one uses null to assign an “empty” or “unknown” value to a variable, while undefined is reserved as a default initial value for unassigned things.


9 Objects and Symbols

9.1 Objects

The object type is special.

All other types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections of data and more complex entities.

let user = {
  name: "John",
  age: 30
};

9.2 Symbols

The symbol type is used to create unique identifiers for objects.

let id = Symbol("id");

Symbols are guaranteed to be unique. Even if we create many symbols with the same description, they are different values.


10 The typeof Operator

The typeof operator returns the type of the operand. It’s useful when we want to process values of different types differently.

A call to typeof x returns a string with the type name:

typeof undefined  // "undefined"
typeof 0          // "number"
typeof 10n        // "bigint"
typeof true       // "boolean"
typeof "foo"      // "string"
typeof Symbol("id") // "symbol"
typeof Math       // "object"
typeof null       // "object"  (officially recognized error)
typeof alert      // "function"

10.1 typeof Quirks

  1. typeof Math returns "object" because Math is a built-in object
  2. typeof null returns "object" - this is an officially recognized error in typeof, kept for compatibility. null is NOT an object.
  3. typeof alert returns "function" - functions belong to the object type, but typeof treats them differently

10.2 typeof Syntax

You may see two syntaxes:

typeof x      // without parentheses
typeof(x)     // with parentheses

Both are the same. typeof is an operator, not a function. The parentheses are for grouping, not function calls.


11 Type Comparison Table

Type typeof returns Example
Number “number” 123, 3.14, Infinity
BigInt “bigint” 123n
String “string” "hello", 'world'
Boolean “boolean” true, false
null “object” ⚠️ null
undefined “undefined” undefined
Object “object” {name: "John"}
Symbol “symbol” Symbol("id")
Function “function” function() {}

12 Examples

12.1 Example 1: Type Checking

let value = 42;
console.log(typeof value); // "number"

value = "Hello";
console.log(typeof value); // "string"

value = true;
console.log(typeof value); // "boolean"

12.2 Example 2: Working with Different Types

let num = 10;
let str = "20";
let bool = true;

console.log(num + num);   // 20 (number addition)
console.log(str + str);   // "2020" (string concatenation)
console.log(num + str);   // "1020" (number converted to string)
console.log(bool + num);  // 11 (true converted to 1)

12.3 Example 3: null vs undefined

let x;
console.log(x);           // undefined (declared but not assigned)
console.log(typeof x);    // "undefined"

let y = null;
console.log(y);           // null (explicitly set to null)
console.log(typeof y);    // "object" (known bug)

12.4 Example 4: Template Literals

let name = "Alice";
let age = 25;
let city = "New York";

// Using template literals
let message = `My name is ${name}, I'm ${age} years old, and I live in ${city}.`;
console.log(message);
// Output: My name is Alice, I'm 25 years old, and I live in New York.

// With expressions
let price = 100;
let tax = 0.15;
console.log(`Total price: $${price + (price * tax)}`);
// Output: Total price: $115

12.5 Example 5: Special Values

console.log(10 / 0);           // Infinity
console.log(-10 / 0);          // -Infinity
console.log("abc" / 2);        // NaN
console.log(NaN + 5);          // NaN
console.log(typeof Infinity);  // "number"
console.log(typeof NaN);       // "number"

13 Summary

  • JavaScript has 8 basic data types
  • 7 primitive types: Number, BigInt, String, Boolean, null, undefined, Symbol
  • 1 reference type: Object
  • JavaScript is dynamically typed - variables can change types
  • Use typeof operator to check the type of a value
  • null represents “nothing” or “empty”
  • undefined represents “not assigned”
  • Strings can use single quotes, double quotes, or backticks (template literals)
  • Special numeric values: Infinity, -Infinity, NaN
  • BigInt for very large integers (append n to the number)
Back to top
JavaScript: Fundamentals
JavaScript: Variables

License: CC0 1.0 Universal

Per aspera, ad astra

  • Edit this page
  • View source
  • Report an issue