The Data Types of Javascript

Javascript has evolved into one of the most robust languages. Before javascript was just mainly focused on adding some interactivity to the web, but now we are able to build mobile application with React Native, backend with Node.js and even desktop ap…


This content originally appeared on DEV Community and was authored by DEV Community

Javascript has evolved into one of the most robust languages. Before javascript was just mainly focused on adding some interactivity to the web, but now we are able to build mobile application with React Native, backend with Node.js and even desktop applications with Electron. To take advantage of all this functionality we need to make sure we have our basic Javascript fundamentals down. Today I want to talk about the different data types in Javascript.

Primitive vs Non-Primitive

There are two main categories data types fall into with javascript. You have primitive and non-primitive. Why is this important? Depending on its category will determine how javascript stores and references the information. A primitive data types stores a single value and are passed by value. A non-primitive data type can store multiple values and are passed by reference.

Primitive

To clear things up, a primitive data type are things like boolean, strings, and numbers. Being passed by value means if you assign one variable the value of a different variable it stores just the value. Example: let x=“hi” let y=x. y is assigned ‘hi’. In background when y is assigned x, Javascript takes 'hi' and creates a new space in memory to reference. This keeps x and y separated from each other.

primitive being passed by value

String

A string is a group of characters inside of quotes. Javascript lets you use both single and double quotes.

let str = hi there
let str2=I have 2 dogs

Number

This represents a number and can be assigned with or without decimals. They are 64-bit floating point values and have a limit to how big a number can be.

let num = 3
let num2 = 3.3

BigInt

This is used to store numbers larger than what the number data type can hold.

let bigNum = 87956239487562934765239847562398745638974562983745623987456

Boolean

Conditional values of true and false. You can set variables to a boolean value or you can also compare and that will return a boolean value.

let bol = true
let num = 3
num === 3 //return true
num === 33 //return false

Undefined

This is when you declare a variable but never assign it a value. Javascript will automatically assign it an undefined value.

let x; //value of x is undefined
let y = undefined //same as declaring without assigning value = undefined

Null

Represents a non-existent or invalid value.

Symbol

Used to store an anonymous and unique value. This a new data type that was introduced with ES6.

let sym = Symbol(unique)

Non-primitive

Non-primitive data types are things like arrays and objects. They can hold multiple values inside of them. Being passed by reference means that if you assign a non-primitive data type to a variable it actually stores the location of the original variable and not the value. This can lead to some tricky errors to fix if not managed correctly. Example let x=[1,2,3] and let y=x. In the example we are not assigning y the value of [1,2,3] like we would for a primitive data type. We are actually assigning it x. In the background when we assign y=x Javascripts looks for the location in memory that represents x. It will then assign that location to y. This means if we make any changes to x it will also reflect in y.

Non-Primitive being passed by reference

Object

Stores a collection of data. If the data type is non-primitive it will always an object type in javascript.

let obj = {name: Tripp, dogDad: true}
let arr = [1,2,3,4]

I hope you found this quick summary of data types in Javascript useful. They are pretty straight forward but if not managed correctly can lead to some tricky errors to debug.


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2021-06-11T20:32:26+00:00) The Data Types of Javascript. Retrieved from https://www.scien.cx/2021/06/11/the-data-types-of-javascript/

MLA
" » The Data Types of Javascript." DEV Community | Sciencx - Friday June 11, 2021, https://www.scien.cx/2021/06/11/the-data-types-of-javascript/
HARVARD
DEV Community | Sciencx Friday June 11, 2021 » The Data Types of Javascript., viewed ,<https://www.scien.cx/2021/06/11/the-data-types-of-javascript/>
VANCOUVER
DEV Community | Sciencx - » The Data Types of Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/11/the-data-types-of-javascript/
CHICAGO
" » The Data Types of Javascript." DEV Community | Sciencx - Accessed . https://www.scien.cx/2021/06/11/the-data-types-of-javascript/
IEEE
" » The Data Types of Javascript." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2021/06/11/the-data-types-of-javascript/. [Accessed: ]
rf:citation
» The Data Types of Javascript | DEV Community | Sciencx | https://www.scien.cx/2021/06/11/the-data-types-of-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.