Data types in JavaScript

Data types in JavaScript

JavaScript is loosely typed and dynamic language which means that the datatype of variable need not to be specified because it is dynamically identified by JavaScript engine which is not the case with programming languages like C, C++, Java where the data type of the object has to be specified while declaration. The below example shows that just mere assignment in JavaScript will store data of any data type in variable.

let a = "apple"  // holding string
let b = 25    // holding number

Note : In JavaScript, a single variable can hold different data types.Check out the example below:

let foo = 5    // Number
foo = "Erik"  // now String
foo = ["apple"] // now Array

To begin operations with variables one must know the data types of the variable as it does not makes sense to "apple" + 25 to perform this addition operation in initial place. Lets dive deep into datatypes in JavaScript. There are two major data types:

  • Primitive (Number, String, Boolean, Undefined, Null)
  • Non - primitive (Array, Object)

Primitive Data Types

1.Number

All the numeric values in JavaScript have number as their data type.

let foo = 10;

In addition, the number data type has three symbolic values

  1. +Infinity
  2. -Infinity
  3. NaN (Not a Number) The only number in JavaScript with two representations is 0. +0 and -0 are its two representations where +0 === -0 is true. Check out the below example
    console.log(30 / 0)  // +Infinity
    console.log(30 / -0)  // -Infinity
    console.log(+0 === -0)  // true
    

    2. String

    String in JavaScript is used to store textual data where every character is stored sequentially at location called as index. Index is used to retrieve the character at specific index which starts from 0
    let foo = "Apple"
    console.log(foo[1])    // "p"
    console.log(foo[foo.length - 1])    // "e"
    
    Length of string is a property used to determine the number of character in the string and is determined by foo.length. Note : Passing negative index like foo[-1] will return undefined Strings are represented using quotes.
    "This is string using double quotes"
    'This is string using single quotes '
    `This is string using back ticks`
    
    Almost anything can be fitted between the two quotes but it becomes difficult to fit special characters like a " " or \n or \t. Well there is a solution to this too i.e escape character. Escape character or \ is inserted just before the special character which tells JS engine that there is a special symbol after it.

Without escape character

console.log(" Hello. \n How are you? ");
// "Hello
// How are you?"

With escape character

console.log(" Hello. \\n How are you? ");
// Hello. \n How are you

Try escaping character for quote too. try here

Mathematical Operations on String

No mathematical operations can be performed on the strings except + which is used to concatenate strings.

let foo = 16 + "mango"  // "16mango"
let foo = "mango" + 16  // "mango16"

While adding number to a string or vice versa the number is converted to string and then concatenated.

let foo = "mango" + 16 + 20    //"mango1620"
let foo = 16 + 20 + "mango"    // "36mango"

Left to right is the order in which JavaScript evaluates the expression which can be clearly seen in above example.

3. Boolean

This datatype is used to distinguish between two possibilites of yes or no/ on or off. Therefore there are only two boolean values i.e true and false

"foo" == "foo"  // true
"one" == "ten"  // false
"NaN" == "NaN"  // false

Note : NaN is the only value in JavaScript which is not equal to itself.

4. Null

A variable which is declared but wanted to be kept as empty deliberately is assigned as null.

let foo = null;
console.log(foo)    // null
typeOf(foo)    // "object" (not "null" for legacy reasons)

5. Undefined

A variable which is declared but not assigned any value will be undefined.

let foo ;
console.log(foo)    // undefined
typeOf(foo)    // "undefined"
console.log(null == undefined);    // true
console.log(null === undefined);    // false (as both have different data type)

Non-primitive Data Type

1. Array

Array are list like data objects in JavaScript which is used to store data at locations called as index.

let foo = ["apple" , "mango", "banana", "orange" ]
console.log(foo[1]);    // "mango"
foo.length    // 4

length property is used to count number of elements present in an array. Some of the common array methods are push() pop() shift() sort() and so on.

2. Object

Object is a collection of data in JavaScript in which data is stored in form of key value pairs. The data is accessed using key.

let foo = {
"firstName" : "John",
"lastName" : "Doe"
}
console.log(foo.firstName)    // "John"
console.log(foo)  //  { firstName" : "John", lastName" : "Doe"}

To delete a key value pair from object delete is used.

let foo = {
"firstName" : "John",
"lastName" : "Doe"
}
foo.delete["lastName"] ;
console.log(foo)    // {"firstName" : "John"}

Before we end ..

I hope you found this article useful and helped you to have more clear idea about the data types in JavaScript. Practice every example you get and keep learning because Knowledge is power and programming is the ocean of challenges.

Let's connect on LinkedIn and Twitter