Data Type in JS
Data Type in JS
JavaScript Variables
In programming, a variable is a container (storage area) to hold data. For example,
let num = 5;
In JavaScript, we use either var or let keyword to declare variables. For example,
var x;
let y;
Both var and let are used to declare variables. However, there are some
differences between them.
var let
var is function scoped (will be discussed in later let is block scoped (will be discussed in
tutorials). later tutorials).
Note: It is recommended we use let instead of var . However, there are a few
browsers that do not support let .
JavaScript Initialize Variables
let x;
x = 5;
let x = 5;
let y = 6;
let x = 5, y = 6, z = 7;
If you use a variable without initializing it, it will have an undefined value.
console.log(x); // undefined
Here x is the variable name and since it does not contain any value, it will be
undefined.
It's possible to change the value stored in the variable. For example,
// 5 is assigned to variable x
let x = 5;
console.log(x); // 5
1. Variable names must start with either a letter, an underscore _ , or the dollar
sign $ . For example,
//valid
let a = 'hello';
let _a = 'hello';
let $a = 'hello';
//invalid
console.log(y); // hi
console.log(Y); // 5
//invalid
Notes:
• Though you can name variables in any way you want, it's a good practice to
give a descriptive variable name. If you are using a variable to store the
number of apples, it better to use apples or numberOfApples rather than x or n .
• In JavaScript, the variable names are generally written in camelCase if it has
multiple words. For example, firstName , annualSalary , etc.
JavaScript Constants
The const keyword was also introduced in the ES6(ES2015) version to create
constants. For example,
const x = 5;
const x = 5;
x = 10; // Error! constant cannot be changed.
console.log(x)
Also, you cannot declare a constant without initializing it. For example,
There are different types of data that we can use in a JavaScript program. For
example,
const x = 5;
const y = "Hello";
Here,
• 5 is an integer data.
• "Hello" is a string data.
JavaScript Data Types
There are eight basic data types in JavaScript. They are:
an integer or a floating-point
Number 3 , 3.234 , 3e-2 etc.
number
Note: The Object data type (non-primitive type) can store collections of data,
whereas primitive data type can only store a single data.
JavaScript String
String is used to store text. In JavaScript, strings are surrounded by double or
single quotes:
• Single quotes: 'Hello'
• Backticks: `Hello`
For example,
//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
Single quotes and double quotes are practically the same and you can use either
of them.
Backticks are generally used when you need to include variables or expressions
into a string. This is done by wrapping variables or expressions with ${variable or
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
A number type can also be +Infinity , -Infinity , and NaN (not a number). For
example,
const number1 = 3/0;
console.log(number1); // Infinity
JavaScript BigInt
In JavaScript, Number type can only represent numbers less than (253 - 1) and more
than -(253 - 1). However, if you need to use a larger number than that, you can use
the BigInt data type.
A BigInt number is created by appending n to the end of an integer. For example,
// BigInt value
const value1 = 900719925124740998n;
Output
900719925124740999n
Uncaught TypeError: Cannot mix BigInt and other types
Note: BigInt was introduced in the newer version of JavaScript and is not
supported by many browsers including Safari. Visit JavaScript BigInt support to
learn more.
JavaScript Boolean
This data type represents logical entities. Boolean represents one of two
values: true or false . It is easier to think of it as a yes/no switch. For example,
You will learn more about booleans in the JavaScript Comparison and Logical
Operators tutorial.
JavaScript undefined
The undefined data type represents value that is not assigned. If a variable is
declared but the value is not assigned, then the value of that variable will
be undefined . For example,
let name;
console.log(name); // undefined
Run Code
A value having the data type Symbol can be referred to as a symbol value. Symbol is
an immutable primitive value that is unique. For example,
Though value1 and value2 both contain 'hello' , they are different as they are of
the Symbol type.
JavaScript Object
An object is a complex data type that allows us to store collections of data. For
example,
const student = {
firstName: 'ram',
lastName: null,
class: 10
};
JavaScript Type
JavaScript is a dynamically typed (loosely typed) language. JavaScript
automatically determines the variables' data type for you.
It also means that a variable can be of one data type and later it can be changed to
another data type. For example,
JavaScript typeof
To find the type of a variable, you can use the typeof operator. For example,
const number = 4;
typeof(number); //returns "number"
const a = null;
typeof(a); // returns "object"
JavaScript Operators
In this tutorial, you will learn about different operators available in JavaScript and
how to use them with the help of examples.
What is an Operator?
In JavaScript, an operator is a special symbol used to perform operations on
operands (values and variables). For example,
2 + 3; // 5
• Assignment Operators
• Arithmetic Operators
• Comparison Operators
• Logical Operators
• Bitwise Operators
• String Operators
• Other Operators
1. JavaScript Assignment Operators
Assignment operators are used to assign values to variables. For example,
const x = 5;
= Assignment operator a = 7; // 7
+= Addition assignment a += 5; // a = a + 5
-= Subtraction Assignment a -= 2; // a = a - 2
*= Multiplication Assignment a *= 3; // a = a * 3
/= Division Assignment a /= 2; // a = a / 2
%= Remainder Assignment a %= 2; // a = a % 2
Note: The commonly used assignment operator is = . You will understand other
assignment operators such as += , -= , *= etc. once we learn arithmetic operators.
const number = 3 + 5; // 8
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Remainder x % y
** Exponentiation (Power) x ** y
// addition
console.log('x + y = ', x + y); // 8
// subtraction
console.log('x - y = ', x - y); // 2
// multiplication
console.log('x * y = ', x * y); // 15
// division
console.log('x / y = ', x / y); // 1.6666666666666667
// remainder
console.log('x % y = ', x % y); // 2
// increment
console.log('++x = ', ++x); // x is now 6
console.log('x++ = ', x++); // prints 6 and then increased to 7
console.log('x = ', x); // 7
// decrement
console.log('--x = ', --x); // x is now 6
console.log('x-- = ', x--); // prints 6 and then decreased to 5
console.log('x = ', x); // 5
//exponentiation
console.log('x ** y =', x ** y);
Run Code
Visit ++ and -- operator to learn more.
Note: The ** operator was introduced in ECMAScript 2016 and some browsers
may not support them.
Here, the comparison operator > is used to compare whether a is greater than b .
Operator Description Example
!= Not equal to: returns true if the operands are not equal x != y
Strict equal to: true if the operands are equal and of the
=== x === y
same type
Strict not equal to: true if the operands are equal but of
!== x !== y
different type or not equal at all
Less than: true if the left operand is less than the right
< x < y
operand
const x = 5, y = 3;
(x < 6) && (y < 5); // true
Here, && is the logical operator AND. Since both x < 6 and y < 5 are true , the result
is true .
// logical OR
console.log(true || false); // true
// logical NOT
console.log(!true); // false
Run Code
Output
true
false
true
false
Operator Description
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
In JavaScript, you can also use the + operator to concatenate (join) two or more
strings.
Example 4: String operators in JavaScript
// concatenation operator
console.log('hello' + 'world');
let a = 'JavaScript';
Output
helloworld
JavaScript tutorial
Here's a list of other operators available in JavaScript. You will learn about these
operators in later tutorials.
(5 > 3) ? 'success' :
?: returns value based on the condition
'error'; // "success"
deletes an object's property, or an
delete delete x
element of an array
result = '3' + 2;
console.log(result) // "32"
let result;
result = '4' * 2;
console.log(result); // 8
result = '4' / 2;
console.log(result); // 2
Run Code
let result;
let result;
result = 4 + true;
console.log(result); // 5
result = 4 + false;
console.log(result); // 4
Run Code
Note: JavaScript considers 0 as false and all non-zero number as true . And,
if true is converted to a number, the result is always 1.
Example 5: null Conversion to Number
// null is 0 when used with number
let result;
result = 4 + null;
console.log(result); // 4
result = 4 - null;
console.log(result); // 4
Run Code
let result;
result = 4 + undefined;
console.log(result); // NaN
result = 4 - undefined;
console.log(result); // NaN
To convert numeric strings and boolean values to numbers, you can use Number() .
For example,
let result;
// string to number
result = Number('324');
console.log(result); // 324
result = Number('324e-1')
console.log(result); // 32.4
// boolean to number
result = Number(true);
console.log(result); // 1
result = Number(false);
console.log(result); // 0
Run Code
result = Number(undefined);
console.log(result); // NaN
result = Number(NaN);
console.log(result); // NaN
Note: You can also generate numbers from strings using parseInt() , parseFloat() ,
result = parseFloat('20.01');
console.log(result); // 20.01
result = +'20.01';
console.log(result); // 20.01
result = Math.floor('20.01');
console.log(result); // 20
To convert other data types to strings, you can use either String() or toString() . For
example,
//number to string
let result;
result = String(324);
console.log(result); // "324"
result = String(undefined);
console.log(result); // "undefined"
result = String(NaN);
console.log(result); // "NaN"
result = String(true);
console.log(result); // "true"
result = String(false);
console.log(result); // "false"
// using toString()
result = (324).toString();
console.log(result); // "324"
result = true.toString();
console.log(result); // "true"
Run Code
Note: String() takes null and undefined and converts them to string.
However, toString() gives error when null are passed.
result = Boolean(0);
console.log(result); // false
result = Boolean(undefined);
console.log(result); // false
result = Boolean(null);
console.log(result); // false
result = Boolean(NaN);
console.log(result); // false
Run Code
result = Boolean('hello');
console.log(result); // true
The table shows the conversion of different values to String, Number, and Boolean
in JavaScript.
String Number Boolean
Value
Conversion Conversion Conversion
1 "1" 1 true
0 "0" 0 false
You will learn about the conversion of objects and arrays to other data types in
later tutorials.