JsGuide

Learn JavaScript with practical tutorials and code examples

Code Snippet Beginner
• Updated Jul 23, 2024

What are JavaScript Data Types - Quick Reference Guide

Complete quick reference for JavaScript data types with practical code examples. Learn primitive and non-primitive types instantly.

What are JavaScript Data Types - Quick Reference Guide

JavaScript data types define what kind of values variables can hold. Understanding JavaScript data types is essential for effective programming and avoiding common type-related errors.

Primitive Data Types #

JavaScript has seven primitive data types that store single values.

1. Number #

Represents both integers and floating-point numbers.

// Number examples let integer = 42; let float = 3.14; let negative = -17; let scientific = 1.5e6; // 1,500,000

console.log(typeof integer); // "number" console.log(typeof float); // "number" console.log(Number.MAX_VALUE); // Largest possible number

2. String #

Represents text data enclosed in quotes.

// String examples let singleQuotes = 'Hello World'; let doubleQuotes = "JavaScript"; let templateLiteral = Current year: ${new Date().getFullYear()}; let emptyString = '';

console.log(typeof singleQuotes); // "string" console.log(templateLiteral);

3. Boolean #

Represents logical values: true or false.

// Boolean examples let isActive = true; let isComplete = false; let comparison = 5 > 3; // true

console.log(typeof isActive); // "boolean" console.log(comparison);

4. Undefined #

Represents variables declared but not assigned a value.

// Undefined examples let declaredOnly; let obj = {};

console.log(typeof declaredOnly); // "undefined" console.log(obj.nonExistentProperty); // undefined

5. Null #

Represents intentional absence of value.

// Null examples let emptyValue = null; let resetValue = null;

console.log(typeof emptyValue); // "object" (known JavaScript quirk) console.log(emptyValue === null); // true

6. Symbol #

Represents unique identifiers (ES6+).

// Symbol examples let symbol1 = Symbol('id'); let symbol2 = Symbol('id'); let globalSymbol = Symbol.for('global');

console.log(typeof symbol1); // "symbol" console.log(symbol1 === symbol2); // false (unique) console.log(Symbol.for('global') === globalSymbol); // true

7. BigInt #

Represents integers larger than Number.MAX_SAFE_INTEGER (ES2020+).

// BigInt examples let bigNumber = 123456789012345678901234567890n; let bigFromNumber = BigInt(123456789);

console.log(typeof bigNumber); // "bigint" console.log(bigNumber); console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991

Non-Primitive Data Type: Object #

Objects store collections of key-value pairs and more complex entities.

Object Literal #

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

console.log(typeof person); // "object" console.log(person.name);

Array #

// Array examples let numbers = 1, 2, 3, 4, 5; let mixed = 'text', 42, true, null;

console.log(typeof numbers); // "object" console.log(Array.isArray(numbers)); // true console.log(numbers.length);

Function #

// Function examples function greet(name) { return Hello, ${name}!; }

let arrow = (x, y) => x + y;

console.log(typeof greet); // "function" console.log(typeof arrow); // "function" console.log(greet('World'));

Type Checking Methods #

typeof Operator #

// typeof examples console.log(typeof 42); // "number" console.log(typeof 'text'); // "string" console.log(typeof true); // "boolean" console.log(typeof undefined); // "undefined" console.log(typeof null); // "object" (quirk) console.log(typeof {}); // "object" console.log(typeof ); // "object" console.log(typeof function(){}); // "function"

Advanced Type Checking #

// Advanced type checking function getType(value) { if (value === null) return 'null'; if (Array.isArray(value)) return 'array'; return typeof value; }

console.log(getType(null)); // "null" console.log(getType()); // "array" console.log(getType({})); // "object" console.log(getType('text')); // "string"

Type Conversion Examples #

Implicit Conversion #

// Implicit type conversion console.log('5' + 3); // "53" (string concatenation) console.log('5' - 3); // 2 (numeric subtraction) console.log(true + 1); // 2 (boolean to number) console.log('5' == 5); // true (loose equality) console.log('5' === 5); // false (strict equality)

Explicit Conversion #

// Explicit type conversion let str = '123'; let num = 456; let bool = true;

console.log(Number(str)); // 123 console.log(String(num)); // "456" console.log(Boolean(0)); // false console.log(Boolean(1)); // true console.log(parseInt('123.45')); // 123 console.log(parseFloat('123.45')); // 123.45

Quick Reference Table #

TypeExampletypeof ResultNotes
Number42, 3.14"number"Integers and floats
String"text", 'hello'"string"Text data
Booleantrue, false"boolean"Logical values
Undefinedundefined"undefined"Unassigned variables
Nullnull"object"Intentional empty value
SymbolSymbol('id')"symbol"Unique identifiers
BigInt123n"bigint"Large integers
Object{}, [], function(){}"object" or "function"Complex data structures

Understanding what JavaScript data types are and how they work is fundamental for writing robust JavaScript applications. Each data type has specific use cases and behaviors that affect how your code executes and performs.

Related Snippets

Snippet Intermediate

Java vs Bedrock Seeds Comparison Utilities

Ready-to-use JavaScript utilities to compare and understand Java and Bedrock seed differences for game development.

#javascript #utilities #gaming +2
View Code
Data Structures
Snippet Beginner

What Are JavaScript Data Types: Detection Utility Functions

Ready-to-use JavaScript utility functions to detect and validate data types with examples for all JavaScript data types.

#javascript #data-types #utilities +2
View Code
Syntax
Snippet Intermediate

What JavaScript Can Do: Ready-to-Use Code Examples

Explore what JavaScript can do with practical code snippets for web development, data processing, API integration, and interactive features.

#javascript #code-snippets #examples +2
View Code
Data Structures
Snippet Beginner

Code to Detect Java vs JavaScript Differences

Are Java and JavaScript the same? Use these code snippets to understand and detect the key differences between Java and JavaScript programming languages.

#java #javascript #comparison +2
View Code
Syntax