📒
SDV503
  • SDV503
  • Command Prompt
    • Windows Command Prompt
    • Mac OS Terminal
  • GIT & GITHub
    • GitHub
    • Git
      • Git Workflow
        • Forking Workflow
  • README
    • How to write a readme.md file?
    • Write a better README.me
    • Generate a README for GitHub Repo Quickly
  • Code-comments
    • Clean Comments
    • Writing Comments in a Good way
  • Pair Coding
    • What is Pair Coding?
    • Pair Programming Experience
  • Programming?
    • What is Programming?
    • What Is A Programming Paradigm?
    • JavaScript Programming Paradigms
  • Number Systems
    • Decimal and Binary numbers
  • JavaSCript
    • Introduction To JavaScript
      • The JavaScript Engine
  • JS Call Stack
    • JavaScript call stack
    • JavaScript & Memory
      • Memory Leaks in JavaScript
    • Execution Context and Execution Stack in Javascript
      • Javascript Execution Context and Hoisting
  • JavaScript Variables
    • Introduction to JS Data Types
    • Primitive and Non-Primitive
    • Operator precedence and associativity
      • JS Operators Part One
      • JS Operators Part Two
      • JS Operators Part Three
    • Clean Code for JS Variables
  • JavaScript Scopes
    • Scope (Chain) Visualized
  • Javascript  — this keyword
  • JavaScript Data Types
    • More JavaScript Data Types
  • JavaScript Expressions and Statements
  • if/else, switch and ternary operator
    • If/Else statement
  • Switch Statement
  • Ternary Operator
  • JavaScript Loops
    • Loops in JavaScript
      • Trouble With Loops
  • Objects
    • JavaScript Objects
      • Prototypal Inheritance Visualized
      • JavaScript Number Object
      • JavaScript String Object
  • Functions
    • JavaScript Function Part One
    • JavaScript Function Part Two
    • Immediately Invoked Function Expressions ~ IIFE
    • JS => Arrow Functions
    • JavaScript Callback
    • Hoisting in JavaScript
      • Hoisting Visualized
    • Recursion Functions
    • Curry and Function Composition
  • JavaScript Features
    • JSpread Operator
    • JavaScript Built-in Functions & Objects
  • Data Structures&Algorithms
    • JavaScript’s Data Types
    • Data Structures in JavaScript
      • Introduction to Big O Notation
      • Big O Notation in Javascript
      • Linked Lists
        • Linked Lists — 2
      • Hash Tables
      • Stack & Queue
  • TLDR
    • Single quotes (‘ ’) and double quotes (“ ”) in JavaScript
  • ES6
    • Generators and Iterators
    • Javascript Classes
    • JavaScript closures
    • JavaScript Promises & Async/Await
      • Event Loop Visualized
  • C#
    • What does C#? (C Sharp)
    • C# vs JavaScript
    • What Is The Difference Between C#, .NET, ASP.NET, Microsoft.NET, and Visual Studio?
    • What is the .NET Framework?
    • Methods and Properties of Console Class in C#
    • Datatypes in C#
    • C# Code Comments
    • The if statement
    • The switch statement
    • Loops
    • Comparison operators
    • Addition assignment operators
    • The String Interpolation Operator
    • Arrays
    • Lists
    • Dictionaries
Powered by GitBook
On this page
  • What are Data Types?
  • Primitive vs Non-Primitive
  • Primitive
  • Non-Primitive (referred to collectively as Objects)
  • Conclusion

Was this helpful?

  1. JavaScript Variables

Primitive and Non-Primitive

What are Data Types?

If you have even a little bit of understanding of code, you should know that there is a variety of different “types” of data.

Certain functions may only be applied to a specific data type.

For example:

// The following is possible:5 * 3 // 15
"ha" + "ppy" // "happy"// The following is not possible:"ben" * 2 // NaN You cannot multiply a string
"hope" - "o" // NaN You cannot subtract a string from another string

At the same time, the different data types are stored differently inside a computer’s memory.

Primitive vs Non-Primitive

Before I go into what the two categories of data types are, I will list which data types belong in which category.

Primitive

  1. Numbers

  2. Strings

  3. Booleans

  4. undefined

  5. null

Non-Primitive (referred to collectively as Objects)

  1. Objects

  2. Arrays

  3. Functions

The two categories represent the two different ways these data types are stored into memory. Primitives are stored by value while Non-Primitives (Objects) are stored by reference.

Let’s look at an example of how Primitives are stored:

let a = 5
let b = a
console.log(a) // 5
console.log(b) // 5
console.log(a === b) // true
a = 10
console.log(a) // 10
console.log(b) // 5
console.log(a === b) // false

What happened here?

We set two different variables: a and b.

a was set to equal 5.

b was set to be equal to a.

Then we changed the value of a to equal 10.

Yet somehow, the value of b was still 5!

This is because Primitives are stored by value.

What this means is that every time we decide to declare a new variable using a primitive data type we are creating a new address in memory for that value.

Let’s look back at our example:

let a = 5
let b = a

The type of data for a is a Number which is a Primitive data type. It has a value of 5.

The type of data for b is also a Number. It has a value of a.

However, what the variable b is storing is not the variable a itself but the value of the variable.

a has a value of 5, so b is storing 5 into its address.

Later on, even if we decide to change the value of a, this will not affect the value that has already been stored inside b.

This is why when we compare the two now, they are no longer equivalent.

Now let’s look at Non-Primitives (Objects):

let a = [10]
let b = a
console.log(a === b) // truea.push(10)
console.log(a) // [10, 10]
console.log(a === b) // true

We set two different variables: a and b.

a was set equal to an array holding a single value: 10.

b was set equal to a.

Comparing a and b returns true.

Next we modified the data inside a.

Yet, comparing the two variables once again we still return true.

The reason for this is due to Non-Primitives (Objects) are stored by reference.

Look at it this way:

let a = [10]
let b = a// the variable a is referencing the address of the array object [10]
// the variable b is referencing the address of the variable a which is pointing to the address of [10]

Basically, we are always storing pointers to an address when working with Non-Primitive data types.

Since this is the case, if we modify any variable which points to the same address we will change the value stored inside the address itself.

let a = []
let b = a
let c = ba.push(1)
console.log(a) // [1]
console.log(b) // [1]
console.log(c) // [1]b.push(2)
console.log(a) // [1, 2]
console.log(b) // [1, 2]
console.log(c) // [1, 2]

Let’s look at this another way:

console.log([10] === [10])

What do you think is the result of the above line of code?

What exactly are we comparing in that line of code?

Are they pointing to the same address?

The answer?

False!

Why is this the case?

While on the surface, they both contain the same value, both are arrays, and both contain a single element of equal value (10).

However, what you’re comparing is not the value but the addresses.

The [10] on the left has a different address in memory than the [10] on the left.

The equality operator (===) is checking if they are pointing to the same address which is why the result is false.

Conclusion

Primitive data types are stored by value. Non-Primitive data types are stored by reference. When declaring a variable, you are generally creating a potential new address.

In the case of storing primitives, that variable is pointing to a new address for that primitive value. When storing a primitive value (even if it’s just a variable that is set to a value), you are storing a value itself into a new address.

In the case of non-primitive objects, the variable is saving a reference to the object. It’s not creating a new address for a value, just a pointer to the object. If you modify a variable pointing to the address, you’re actually modifying the data stored inside the address itself.

PreviousIntroduction to JS Data TypesNextOperator precedence and associativity

Last updated 4 years ago

Was this helpful?