Data types

·

2 min read

Data can be in any form like a number, text (string), symbol, boolean value (true and false) and a lot of other formats, but in javascript, they are categorised into 7 different primitive data types and one non-primitive data type.

Primitive data types:

Primitive data types are a set of basic data types from which all other data types are constructed. There are 7 primitive data types in javascript. A lot of these data types are self-descriptive .they can tell which data type it is just by reading it.

  1. Number: number data type stores the number. You don’t need to put these inside of double quotes. e.g. let directions=4

  2. String: It is used to store text values. You need to put the value inside of double or single quotes or it won’t perform like a text value. E.g. let name=“Cristiano”

  3. Null: rather than keeping your data type undefined you can use this as a placeholder for upcoming values. E.g. let futureValue=null

  4. Symbol: it is used to store symbols. E.g. const dollarSign = $

  5. Boolean: it is used to store boolean values(true or false). this data type is mostly used for comparison. E.g. const isEarthSphere = true

  6. Bigint: used to store big numbers like 1.7976931348623157e+308 (these is the biggest number javascript can handle )

  7. Undefined: these are the data type which can be used to declare variables for future use. E.g. let noValue.

Non-primitive data type:

There is only one non-primitive data type in javascript and it is OBJECT. To understand objects more clearly you can think of them as a dictionary.

Objects are key-value pairs separated by commas. Objects are written inside of curly braces. you can store all primitive data types inside an object.

const userInfo ={
userName ="Kirat Vohli",
userAge= 34,
}

How to check the data type of an existing variable:

You can use typeof() operator in javascript to find the data type of a variable. Usually operators don’t have any data type but the data type of a typeof() operator is a string.

It also returns the data type of a function and object.

Difference between null and undefined :

one uses null to assign an “empty” or “unknown” value to a variable, while undefined is reserved as a default initial value for unassigned things. null is an assigned value. It means nothing.

Undefined means a variable has been declared but not defined yet.