Data Types in JavaScript
Data types are a fundamental concept of JavaScript. Let's learn together.
JavaScript Data Types in JavaScript
In this tutorial, you will learn all about data types in JavaScript with examples
There are so many data types in JavaScript. But let's look at some of them.
var a = 5; // 5 is a number
let greet = 'hello'; // hello is here text or string'
const message = `hey let's go outskirts tomorrow.` // a simple simple sentence with `backtick`
From the above code snippet, we can see lots of things, var
, let
, const
, number
, strings
, etc. Today's topic lies all around this.
JavaScript Data Types
Data Types | Description | Few Examples |
Number | Any integer or floating value is treated as a number in JavaScript | 5 or 5.55556 |
String | Any character , text or sentence also called ad String | Hello , Simon, go back! |
Boolean | In JavaScript, true or false are treated as Boolean. | true or false |
undefined | Generally, undefined is a data type for those whose value is not initialized | undefined |
null | null is treated as the empty value in JavaScript | null |
BigInt | This is also a number but with arbitrary precision. | 7777777777777777n or 45.66666666666666n |
Symbol | Instance of Symbol is unique and immutable | let IO = Symbol('Abhishek') |
Object | representation of key and value in the close block is known as an object. | let student ={key: 1, key:2} |
BigInt : Value suffixes with
n
here are treated asBigInt
.
BigInt
andSymbol
are introduced in a newer version of JavaScript.
Symbol
are always unique.
object
key and value representation.Except
object
all are primitive data types. Onlyobject
is treated as anon-primitive
type.Primitive Data Type
holds only one type of data. ButNon-Primitive Data
types hold sets of data in them. For examplelet array = [ 1, 2, 3, 'hello' , true ]
Hope we got some idea now about data types. Let's dig inside more in-depth one by one now.
1. Number
Any integer
, float
, fraction
, or exponential
values are treated as numbers.
var num1 = 400; // integer or positive integer
var num2 = -500; //negative integer
var num3 = 20.52; // float or positive float value
var num4 = -20.52; //negative float value
var num5 = 0x000; // hexadecimal
var num6 = 256e-5; // exponential
var num7 = 050; // octal
var num8 = 0c0010001; // binary
2. String
Any character or textual format literals are called String. Strings can be assigned by using three ways:
single quote
- 'hello'double quote
- "hello"- one is using
backticks``
let a = 'd' // using single quote
let subject = "math" // using double quote
let message = `Visit India. It's a beautiful place in the world.` // using backticks
3. Boolean
In JavaScript, Boolean data types support either true
or false
. Any condition
or statement
can be either truthy
or falsely
. 0
is treated as false
and 1
is treated as true.
let user = true;
console.log(user) // true
let password = false;
console.log(password) // false
4. undefined
Implicitly, any variable with no value assigned to that variable is always undefined
. In the backend, all variables are first assigned to an undefined
value. if we try to access any variable before assigning the value is undefined
console.log(user) // undefined
let user;
we can also explicitly assign undefined to any variable.
let user = undefined
5. null
null
is treated as the empty value in JavaScript. Variable assigned with null
means that variable is empty or unknown.
let count = null; // null is assigned to variable count
6. BigInt
If you wish to store the large number in JavaScript we can use BigInt
data type. It is appended by n
.
For example
let largeNum = 122222222222223333333n; // appended by `n`
let lgNum = 1n;
Remember not all browser support BigInt now.
7. Symbol
Symbol
is used to store unique values. values with the same description are treated as unique in JavaScript.
For Example
// This is an example of a Symbol Data type with unique values but with the same description.
let name1 = Symbol('abhishek') // Unique
let name2 = Symbol('abhishek') // Unique
Here, name1
and name2
both are unique. It can be a used case to provide a unique id to the user but it's not recommended. Just a fun fact !!!๐.
8. Object
Arrays
and Objects
are non-primitive types and can store sets of values.
For example
let states = [ 'Karanataka' , 'Jaipur', true , 1 , 0 , undefined, null ];
let students = {
fname: 'abhishek',
lname: 'sharma',
course: 'fsjs',
contact: 'abhishek7329sharma@gmail.com'
mobile: 911XXXXXXX
}
// representation of keys and values in a single entity is called an object.
Here, students is an object. Keys are fname
, lname
, course
, etc.
Values are abhishek
, sharma
, fsjs
, etc.
typeof
There is one more important thing we should learn if we have come so far that is typeof. It's important that we should learn how to find the type of any variable.
let name = 'John'
let value = 1
console.log(typeof(name)) // String
console.log(typeof value) // Number
// similarly we can get type of any variable like this
Remember we can use typeof with both parenthesis and without parenthesis.
If you are reading this still CONGRATULLATIONS!!! ๐ฅณ you have come so far and understood a lot in this tutorial about data types. If you find it meaningful then ๐ like this blog motivate me to share more concepts about JavaScript in the future.
Thanks!!! ๐๐๐๐ Bye.