Data Types In JavaScript

DevJunctionPoint
0

What are Data Types?

In JavaScript, Data types are defined as what type of value can be stored inside the variables.

There are two types of data types in JavaScript.
  • primitive datatypes(predefined)
  • non-primitive data type (reference datatype)
Primitive Datatypes:
  • primitive data types are immutable(non-modifiable) data types. 
  • Once a primitive data type is created, we cannot modify it.
There are seven primitive types in JavaScript
  1. Numbers - Integers, floats
  2. Strings - Any data under single quote, double quote, or backtick quote
  3. Booleans - true or false value
  4. Null - empty value or no value
  5. Undefined - a declared variable without a value
  6. Symbol - A unique value that can be generated by the Symbol constructor
  7. BigInt
Numbers:
  • Numbers are integers and decimal values which can do all the arithmetic operations.
example:


// Number data type

let age = 35
const PI = 3.14  // pi a geometrical constant
console.log("age = ", age) // age value = 35
console.log("age type = ", typeof (age)); // Number

console.log("PI value = ", +PI); // PI Value = 3.14
console.log("PI value = ", typeof (PI)); // Number



String
  • Strings are used to represent textual data, such as a sequence of characters. 
    • Single quotes: 'Hello'
    • Double quotes: "Hello"
    • Backticks: `Hello`
// string

let space = ' ' // an empty space string
let language = 'JavaScript'

console.log(space)
console.log("language = ", language); // JavaScript
console.log("type of language = ", typeof (language));  // String


  • Single quotes and double quotes are practically the same and you can use either of them.
  • Primitive data types are compared by their values. Let us compare different data values. See the example below
let numOne = 3
let numTwo = 3

console.log(numOne == numTwo)      // true

let js = 'JavaScript'
let py = 'Python'

console.log(js == py)             //false

let lightOn = true
let lightOff = false

console.log(lightOn == lightOff) // false


Boolean:
  • It is used to represent a boolean value, which can be either true or false.

// Boolean

const isEligible = true;
console.log(isEligible) // true


Undefined:
  • It represents a value that is not yet defined or initialized.


let name;
console.log(name); // undefined
console.log(typeof(name)) // undefined


Null:
  • null data type means an empty value assigned to a variable

const age = null;
console.log(age);
console.log(typeof(age)) // object


Symbol:
  • Symbols in JavaScript are used to represent a unique and immutable value.

const firstName = symbol('sai');
const lastName = symbol('veny');
console.log(firstName + ' ' + lastName)



BigInt:
  • BigInt is a new numeric data type that can represent integers with arbitrary precision.


bigNum = 1234455566671n;
console.log(bigNum);



Non-primitive data types
  • Non-primitive data types are modifiable or mutable.
  • We can modify the value of non-primitive data types after it gets created.
  • Non-primitive data types cannot be compared by value
There are 2 Non-Primitive data types

  1. An object
  2. An array

1. Array
  • An array is a list of data values in a square bracket.
  • Arrays can contain the same or different data types.
  • Array values are referenced by their index.
  • The array index starts at zero. the second element at index one, and the third element at index two, etc.
    
    // creating array.
    let nums = [1, 2, 3]
    // changing array value 1 to 10 --> index 0 is 1
    nums[0] = 10
    //Outout
    console.log(nums)  // [10, 2, 3]


  • As you can see, an array, which is a non-primitive data type is mutable
  • Non-primitive data types cannot be compared by value. Even if two non-primitive data types have the same properties and values, they are not strictly equal
 
  let  num1 = [1, 2, 3]
let num2 = [1, 2, 3]
console.log(num1 == num1)  // false
  • we do not compare non-primitive data types arrays, functions, or objects.
  • Non-primitive values are referred to as reference types because they are being compared by reference instead of value.
  • Two objects are only strictly equal if they refer to the same underlying object.
  
    let nums = [1, 2, 3]
   let numbers = nums

   console.log(nums == numbers)  // true


Object:
  • Collection of value in the form of key-value pairs which can include properties and methods.
    
    let user = {

        name: "learn",
        lastname: "javascript",
        developer: true,
        age: 20
    }

    console.log(user);
    console.log(typeof user);



Tags

Post a Comment

0Comments

Do leave your comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!
To Top