What is the difference between reference and primitive data types types in Javascript?

JavaScript: What is the difference between reference and primitive data types types in Javascript?

There are two main types of data types in JS - reference and primitive. Here is a quick overview of the differences

Reference Data Types

Reference data types are accessed by reference - it is a pointer to a location in memory. Their data is stored on the heap. If it is an object, it is a reference type.

Some of the main references types are:

Object literals, Arrays, Functions, etc

Primitive Data Types

This would be the typical normal way of storing data, by value. It is stored on the stack.

There are 6 main primitive types:

String, Number (there is no real distinction between floats, integers in JS. They are all stored as floats.), Null, Boolean, Undefined and Symbols (since ES6)

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about What is the difference between reference and primitive data types types in Javascript?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the JavaScript language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to find the intersection (same values) from two arrays?

Push vs Pop vs Shift vs Unshift

How to generate an array for a dropdown (with ['key' => 'value']) suitable for a dropdown, from a Laravel collection of Eloquent objects?

How to return JSON data to the user in Laravel?

How to set up custom error pages in .htaccess for your Apache server (ErrorDocument)

How to block IP addresses in Apache by using regular expressions (regex) in .htaccess

Force trailing slash (redirect if no trailing slash exists) in .htaccess

How can you check if a variable is alphanum (alphanumeric, letters and numbers only) in PHP?

What are Template literals (Template strings) in JS?

PHP Date commonly used formats