JavaScript Data Types – Primitive Vs Objects

Here we will discuss the data types used in JavaScript. As we know all the programming langauges have a set of data types that help languages represt, write and execute codes. Even though JavaScript is popularly known as untyped lanagaue it does not tantamount to it lacks actual data types. It has them as well.
According to ECMAScript there are as many as 7 data types in JavaScript. We are going to discuss all of these data types in detail. So keep reading along.

(A) Primitive Types
1- Numbers
2- Strings
3- Booleans
4- Null
5- Undefined
6- Symbols
(B) Object types

Primitive

The primitive data type is kind of not something that its literal meaning echoes. The primitive data type in JavaScript means that does not adhere to any method or is not an object. We cannot change their values as they remain immutable once we decalre them.

Numbers

JavaScript also has the number data types which is the only numeral data types–double float. So we could use them without having to convert data types for any type of arithematic needs. See the example here

console.log(5/2);
// returns 2.5

It depends on the code what we are using it for, it could be for a float or an integer. We can use it for the both.

// Integer literal
10
100
1235845621

//Float literal
10.278
8.3e4
.564

Strings

Like other langauges, JavaScript also has string data types that we use in script to express textual data. A string should be closed with quotes or double quotes it also depends upon the use. The strings have elements that are defined as per their index positions. For example we place an element at index 0 and the other at index 1, this is how it works. The elements in a string define the length of string.
See the example here:

'I am a string'
"I am another string"

Using quotation marks in a string pretty much defines the string and these quotation could be even used inside a string. We can also use backslash \ to represent an escape sequence in JavaScript. See the example here which defines it:

'I\'m coding for my new project \'JavaScript\''
// I'm coding for my new project 'JavaScript'

There are also other instances of escape backslash which are ‘ single quote ” double quote \ backslash \n new line \r carriage return \t tab \b backspace. Later we will use them in other posts to clear the air

// Backslash code with multiple lines
"This is \
a \
string"

// String interpretation with multiple lines
"This is \na \nstring"

String template literal
Template literals are string literals that allows developer to embed expressions in a string. They are marked by the enclosing back-tick mark “` that surrounds them.
The template literals are used to embed expressions inside a string they are also literal string types.

// template literals can also be used to create multi line string interpretation without using \n
`This is
a
string`

The template literals can also use ${} to enclose the variables as these expressions/variables can be used by them as string elements. Here is one example to demontrate it:

let name = "Brad";

`The actor is ${name} Pitt, for the new action movie.`
// normal string equivalent
'The actor is ' + name + ' Pitt, for the new action movie.'

// Using expressions
`The result of 12 + 22 is ${12+22}`

We did not discuss Object in a string in JavaScript that’s for later

Boolean data type

Boolean type of data in every single of the programming langauges demonstrate two value: true and false and JavaScript has the same. We can use Boolean data type for different comparative operations such as == or != in order to get a Boolean value. See the example here

8 == 8
7 != 10
9 >= 20 // ..is bigger or equal to..
"Brad" != "Pitt" // will compare unicode values

There’s also a notion of falsy values in JavaScript and will return false in Boolean context. This example pretty some that up well

//They are alwasy falsy(false)
false
0
'' or "" // it's an empty string
null
undefined
NaN //e.g. the result of 1/0

While all other values are absolutely truthy (true) values

'0' // when a string has a just zero
'false' //when a string having the text "false"
[] //an empty array
{} //an empty object
function(){} // an empty function 

Null Data Type

Null data type is used to represent an absence of any value. See the example below for the Null use-case in JavaScript

let name = null;

Undefined

We will always get ‘undefined’ as a return value by either variable/function which show they are not defined or given value

let name;
console.log(name); 

Object

Object data type in JavaScript are not primitive by nature. The properties and methods distinguish them from the primitive data types. Object data type is written with Capital-first letter.

The most common examples of Object data types include arrays and strings. Did not we mention string is a primitive data type? Yes we did. On the contrary, JavaScript comes with built-in objects that we may use when needed and String is one such object which is built-in and that can be used as such as well.
So when we compile our JavaScript code they are converted into from primitive to String Objects. It helps us use them ‘string objects’ for primitive strings as well. See the example to diturp you thoughts and clear the air

"This is a string".length
"Another string here".charAt(4) 

For a detailed analysis of string data types visit Mozzila Documentation.