Data Types in JavaScript

JavaScript data types, JavaScript string number boolean object

Primitive Data Types

  1. String"Hello"
  2. Number42, 3.14
  3. Booleantrue or false
  4. Undefined – declared but no value assigned
  5. Null – intentional empty value
  6. Symbol – unique identifiers
  7. BigInt – for very large integers
  8. (11) YouTube
  9. Build a To-Do List App in Python – Console-Based Project for Beginners – 804

Non-Primitive Data Types

  • Object
javascriptCopyEditlet person = { name: "Ali", age: 25 };
  • Array
javascriptCopyEditlet fruits = ["apple", "banana"];
  • Function
javascriptCopyEditfunction greet() { console.log("Hi!"); }

Type Conversion

Implicit:

javascriptCopyEditconsole.log("5" + 2); // "52"

Explicit:

javascriptCopyEditNumber("5"); // 5
String(123); // "123"

typeof Operator

javascriptCopyEdittypeof "Hello"; // string
typeof 42; // number

🌐 JavaScript Data Types: Full Guide

JavaScript is a dynamically typed language, meaning you don’t need to specify variable types. The type is determined automatically at runtime.


🔹 1. Primitive Data Types

Primitive types are the most basic data types in JavaScript. They are immutable and represent a single value.

🟡 a. Number

  • Represents both integer and floating-point numbers.
  • Includes special values: Infinity, -Infinity, and NaN (Not-a-Number).
let age = 25;
let price = 19.99;
let temperature = -5;
let notANumber = 0 / 0; // NaN

🟡 b. String

  • Represents a sequence of characters.
  • Enclosed in single ('), double ("), or backticks (`\) for template literals.
let name = "Alice";
let greeting = 'Hello';
let message = `Welcome, ${name}!`; // Template literal

🟡 c. Boolean

  • Represents one of two values: true or false.
let isLoggedIn = true;
let hasPermission = false;

🟡 d. Undefined

  • A variable that has been declared but not assigned a value is undefined.
let x;
console.log(x); // undefined

🟡 e. Null

  • Represents an intentional absence of any value.
let selectedItem = null;

Note: typeof null returns "object" – this is a well-known quirk in JavaScript.


🟡 f. Symbol (ES6)

  • Represents a unique and immutable identifier.
  • Often used to create hidden object properties.
let sym1 = Symbol("id");
let sym2 = Symbol("id");
console.log(sym1 === sym2); // false

🟡 g. BigInt (ES11)

  • Used for arbitrarily large integers beyond the safe limit of the Number type.
let bigNumber = 9007199254740991n;
let anotherBig = BigInt("123456789012345678901234567890");

🔸 2. Non-Primitive (Reference) Data Types

These types can store collections of values or more complex entities. They are mutable and stored by reference.


🔷 a. Object

  • The most important complex data type in JavaScript.
  • Stores key-value pairs.
let person = {
  name: "John",
  age: 30,
  isEmployed: true
};

🔷 b. Array

  • A special kind of object used for storing ordered collections.
let colors = ["red", "green", "blue"];
let numbers = [1, 2, 3, 4, 5];

🔷 c. Function

  • A function is also an object in JavaScript.
function greet(name) {
  return `Hello, ${name}`;
}

Or as an expression:

let greet = function(name) {
  return `Hi, ${name}`;
};

Or with arrow syntax:

let greet = (name) => `Hey, ${name}`;

🔷 d. Date, RegExp, Map, Set, WeakMap, WeakSet, etc.

These are built-in object types in JavaScript.

  • Date: Work with dates and times.
  • RegExp: Pattern matching with regular expressions.
  • Map: Key-value pairs with any data type as keys.
  • Set: Stores unique values.
  • WeakMap and WeakSet: Similar to Map and Set but with weak references (used for memory management).
let now = new Date();
let pattern = /hello/i;
let map = new Map();
map.set("key1", "value1");

🧠 Dynamic Typing Example

JavaScript allows you to change the type of a variable during runtime:

let data = 42;      // number
data = "hello";     // string
data = true;        // boolean

🔍 typeof Operator

Use typeof to check the type of a variable:

console.log(typeof 123);        // "number"
console.log(typeof "abc");      // "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"

📌 Summary Table

TypeCategoryExampletypeof result
NumberPrimitive42, 3.14, NaN"number"
StringPrimitive"hello", 'world'"string"
BooleanPrimitivetrue, false"boolean"
UndefinedPrimitivelet x;"undefined"
NullPrimitivenull"object"
SymbolPrimitiveSymbol("id")"symbol"
BigIntPrimitive12345678901234567890n"bigint"
ObjectReference{ name: "Alice" }"object"
ArrayReference[1, 2, 3]"object"
FunctionReferencefunction() {}"function"

✅ Best Practices

  • Use const or let instead of var.
  • Avoid comparing different data types with ==; use === (strict equality).
  • Prefer using Array.isArray() instead of typeof for arrays.
  • Be cautious with null and undefined.

Leave a Reply

Your email address will not be published. Required fields are marked *

Magic Moments Early Learning

Received overcame oh sensible so at an. Formed do change merely.

Category

Latest posts

  • All Posts
  • Adventures
  • App in Python
  • Beginner Projects
  • Beginner Tutorials
  • Blog
  • Computer Science Basics
  • Creations
  • Hardware & Architecture
  • javascript
  • Learning
  • Programming
  • Python for Beginners
  • startcomputersceince
  • Storybook

Tags

Contact Info

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Edit Template

About Our School

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis.

About School

About Us

Services

Community

Testimonial

Help Centre

Quick Links

Classes

Events

Programs

Become Teacher

Contact Us

© 2023 Created with Royal Elementor Addons