What are Datatypes?
In programming, data types is an important concept.
To be able to operate on variables, it is important to know something about the type.
For eg, 2 and 3 are numbers and when we add them, we get 5.
JavaScript is a loosely typed and dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types.
In Javascript, there are 7 primitive datatypes:
Boolean
Boolean represents a logical entity and can have two values: true
and false
.
var a = true //Variable 'a' holds value of boolean type.
Number
The number type represents both integer and floating point numbers (decimals and exponentials).
A variable of number type can range between [-(253 − 1),(253 − 1)].
In addition to representing floating-point numbers, the number type has three symbolic values: +Infinity
, -Infinity
, and NaN
(“Not a Number”).
var a = 2.3 //Variable 'a' holds value of number type.
var b = 2 //Variable 'b' holds value of number type.
String
String type is used to represent textual data. A string in JavaScript must be surrounded by quotes(double, single or backticks). It is a set of elements and each element in the string occupies a position.
var a = "Hello"; //Variable 'a' holds value of string type
Null
The Null type has exactly one value: null
. Its a special value which can represent “nothing”, “empty” or “value unknown”.
var a = null //Variable 'a' holds a value of null type.
Undefined
A variable that has been declared but not been assigned a value has the value undefined
.
var a; //Variable 'a' holds undefined
var b = undefined; //Variable 'b' holds undefined
BigInt
BigInt type has been recently added to the language to represent integers of arbitrary length (numbers which cannot be represented in number
type.
A BigInt value is created by appending n
to the end of an integer. For e.g, 23n
is a value of bigint type.
var a = 9324012456789012613321123n;
Symbol
A Symbol is a unique and immutable primitive value and may be used as the key of an Object property. It is used to create unique identifiers for objects.
Some more Datatypes:
Object
Object refers to a data structure containing data and instructions for working with the data. Objects sometimes refer to real-world things, for e.g, a vehicle etc.
Objects can contain many values in form of key-value pairs.
JavaScript objects are written with curly braces {}.
var a = { //Variable 'a' stores an object which contains several key-value pairs.
name: "Max",
age: 20
}
Function
A function is a code snippet that can be called by other code or by itself, or a variable that refers to the function.
var a = function () { //Variable 'a' holds a function
console.log("Hello world!");
};
typeof Keyword
typeof
is an operator which can easily help us to find the type of a certain variable.
For e.g console.log(typeof 30)
prints number
as output, signifying that the value 30 belongs to number type.
Type Conversion
JavaScript variables can be converted to a new variable and another data type by the use of a JavaScript function.
var a = true;
var b = String(true);
console.log(typeof a); //prints boolean
console.log(typeof b); //prints string
Try the following example in the editor below.
Given an input string which represents a positive number, you have to print the next greater integer.
Sample Input</div>
123
Sample Output</div>
124
Hint
We can the next greater number by using ‘+’ operator. For e.g. console.log(5+1)
prints 6
.