Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Primitive vs Reference Types | Understanding JavaScript Data Types
Quizzes & Challenges
Quizzes
Challenges
/
JavaScript Data Types Foundations

bookPrimitive vs Reference Types

Understanding how JavaScript handles data types is crucial for writing reliable code. JavaScript divides its data types into two main categories: primitive types and reference types.

Primitive types are the most basic units of data. They include string, number, boolean, null, undefined, and symbol. These types are immutable, meaning their values cannot be changed once created. When you assign a primitive value from one variable to another, JavaScript copies the actual value. This means changing the value in one variable does not affect the other.

Reference types include objects, arrays, and functions. These are more complex structures. Instead of copying the actual value, JavaScript copies a reference (or pointer) to the location in memory where the data is stored. If you change the contents of a reference type through one variable, the change is visible through any other variable that references the same object.

12345678910111213
// Primitive type assignment let a = 10; let b = a; // b gets a copy of a's value b = 20; console.log(a); // Output: 10 (a is unchanged) console.log(b); // Output: 20 // Reference type assignment let obj1 = { value: 10 }; let obj2 = obj1; // obj2 references the same object as obj1 obj2.value = 20; console.log(obj1.value); // Output: 20 (obj1 is changed) console.log(obj2.value); // Output: 20
copy

Think of primitive types like making a photocopy of a document: if you write on your copy, the original stays the same. Reference types are more like sharing a link to a collaborative document: if anyone edits the document, everyone with the link sees the change. This distinction helps you predict how your data will behave when assigned or passed around in your code.

question mark

Which of the following is a primitive type in JavaScript?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 6.25

bookPrimitive vs Reference Types

Scorri per mostrare il menu

Understanding how JavaScript handles data types is crucial for writing reliable code. JavaScript divides its data types into two main categories: primitive types and reference types.

Primitive types are the most basic units of data. They include string, number, boolean, null, undefined, and symbol. These types are immutable, meaning their values cannot be changed once created. When you assign a primitive value from one variable to another, JavaScript copies the actual value. This means changing the value in one variable does not affect the other.

Reference types include objects, arrays, and functions. These are more complex structures. Instead of copying the actual value, JavaScript copies a reference (or pointer) to the location in memory where the data is stored. If you change the contents of a reference type through one variable, the change is visible through any other variable that references the same object.

12345678910111213
// Primitive type assignment let a = 10; let b = a; // b gets a copy of a's value b = 20; console.log(a); // Output: 10 (a is unchanged) console.log(b); // Output: 20 // Reference type assignment let obj1 = { value: 10 }; let obj2 = obj1; // obj2 references the same object as obj1 obj2.value = 20; console.log(obj1.value); // Output: 20 (obj1 is changed) console.log(obj2.value); // Output: 20
copy

Think of primitive types like making a photocopy of a document: if you write on your copy, the original stays the same. Reference types are more like sharing a link to a collaborative document: if anyone edits the document, everyone with the link sees the change. This distinction helps you predict how your data will behave when assigned or passed around in your code.

question mark

Which of the following is a primitive type in JavaScript?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2
some-alt