Data Types in JavaScript!!!

Data Types in JavaScript!!!

·

2 min read

Every Programing language has some built-in data structures, but these data structures differ from one language to another.

Today we are going to talk about JavaScript data types.

giphy.gif

There are two types of data structures in JavaScript.

Primitive Data Types

  • String
  • Number
  • Boolean
  • BigInt
  • Undefined
  • Null
  • Symbol

Non-Primitive Data Types

  • Object

String

String type is used for storing text. Strings can be surrounded by quotes.

For example:

const a = 'Hello world!'; //single quotes
const b = "Hello World!" //double quotes
const c =`My first line of code was ${a}` //backticks

//we can also find the length of a string by using the built-in length property

const len = c.length;

Number

Number type is used for storing both integer and floating-point numbers.

For example:

const x = 5;
const y = 5.567;
const z =3e-2;

Boolean

Boolean type represents any values true or false.

For example:

const a = true;
const b = false;

BigInt

BigInt type represents any values or numbers less than (253 - 1). BigInt primitive, created by appending n to the end of an integer literal, or by calling the BigInt().

For example:

const bigNumber = BigInt(12346574987646987987987648)

//output: 12346574987646987233394688n

const bigHexNumber = BigInt("0x2fffffffffffff")

//output 13510798882111487n

const largeBinary = BigInt("0b11111111111111111111111111111111111111111111111111000")

//output 9007199254740984n

Undefined

Undefined is a type whose value s not initialized.

For example:

const x;
//output undefined

Null

The null type represents a special value that is empty or unknown.

For example:

const x = null; // null is not same as NULL or Null

//output null

Symbol

A Symbol is a type that represents unique and immutable. In some programming languages, it is called "atoms".

For example:

const sym = Symbol("Hello")

//output  Symbol(Hello)

Object

An object is a non-primitive complex data type. It is used to store collections of data.

const obj ={
    firstName:"Jhonny",
    lastName:"Depp",
    age:58,
   phoneNumber:null,
}
//to see the output

console.log(obj)

To find the type of a variable we can use typeof

For example:

const a = "Hello world"
console.log(typeof(a)) //output string

const b = 58
console.log(typeof(b)) //output number

const c = null
console.log(typeof(c)) //output object because null is "nothing". It is supposed to be something that doesn't exist. Unfortunately, in JavaScript, the data type of null is an object

Thank You very much for reading this article!!!❤️

office.gif

Find me on Twitter