Hey there :) in the last post we have understood how comments are used and variables are declared in JavaScript. In this post we are going to see the data types in JavaScript. So let's get started.
JavaScript provides two different types of data types. They are->
Primitive Data Types
Non Primitive Data Types
Let's see each one of them individually.
Note -: In JavaScript ,as it is dynamically typed language you don't need to bother about specifying data type of variable ,you just simply have to assign them as
var
,let
orconst
according to your requirement.
Primitive Data Types
*Primitive data types are basic data types that are not objects and do not have methods. * They are immutable and do not have their own methods and properties. When you manipulate a primitive value, you're working directly with its value rather than with a reference to it. Primitive values are stored directly in memory, and when you assign a variable to a primitive value, the variable stores a copy of that value.
There are six primitive data types:
String: Represents a sequence of characters, such as "hello".
Number: Represents both integer and floating-point numbers, such as 42 or 3.14.
Boolean: Represents a logical entity, either true or false.
Null: Represents null i.e. no value at all.
Undefined: Represents a variable that has been declared but not assigned a value.
Symbol: Represents a unique identifier, introduced in ECMAScript 6 (ES6).
Non Primitive Data Types
Non-primitive data types in JavaScript are data types that are capable of storing collections of values and have methods and properties. They are mutable, meaning their values can be changed.
The main non-primitive data types in JavaScript are:
Object: Objects are complex data types that can hold key-value pairs. They are collections of properties where each property has a name and a value. Objects can represent various entities and can contain functions, arrays, and other objects as values.
const person = {
name: "John",
age: 30,
hobbies: ["reading", "coding"],
address: {
city: "New York",
country: "USA"
},
sayHello: function() {
console.log("Hello!");
}
};
Here person is object having name,age,hobbies and address as properties andsayHello
as method.Array: Arrays are ordered collections of values. They can store multiple values of any type, including other arrays and objects. Arrays are indexed, meaning each value is associated with a numeric index starting from 0.
const fruits = ["apple", "banana", "orange"];
Function: Functions are a special type of object that can be invoked to perform a task or calculate a value. Functions can be assigned to variables, stored in objects or arrays, and passed as arguments to other functions.
function add(a, b) {
return a + b;
}Date: The
Date
object represents a specific date and time in JavaScript. It provides methods for working with dates and times, such as getting the current date, formatting dates, and performing arithmetic operations on dates.
const currentDate = new Date();
So this was it for this post in the next post we will be reading about operators and conditional statements. Till then stay connected and don't forget to follow me :)
Top comments (6)
There are actually 7 primitive data types, you've missed
BigInt
.Thanku so much for taking this into my account :)
Love this review series on Javascript @akshat0610! π Taking it day by day in your articles also makes it easier to digest the information in more bite-sized chunks- excellent idea!
keep learning!
Nice Post! But would appreciate it if you'd use the code tag while writing the markdown in this post. That would be beneficial for other readers of this post.
Great job by the way! π
I'd definitely use