In the last few lectures, we discuss Values & Variables and Convention and Rules for naming Variables. Values can have different types in every programming language, depending on the kind of data we want them to hold. And we already saw strings
and numbers
but there are more data types. So, let's now take a look at all the data types we have in the javascript.
First of all, in javascript, every value is either an Object
or Primitive
value. We will learn all about Objects later but for now, let's focus on Primitive data types.
let me = {
firstName: 'Akhil',
secondName: 'Naidu',
age: 24
};
let firstName = 'Akhil';
let secondName = 'Naidu';
let age = 32
Primitive Data Types
There are seven primitive data types, number
, string
, boolean
, undefined
, null
, symbol
, and BigInt
. So let's look at them one by one.
Number
The numbers are always so-called floating-point numbers, which means that they always have decimals, even if we don't see them or don't define them.
let age = 23;
// You can use this as a test to check it
console.log(100/3) // 33.333333333333336
In many other programming languages, you will find different data types for Integers and Decimals, but not in javascript. In javascript, both integers and decimals come under a single number
datatype.
Strings
Strings are nothing but a sequence of characters, and so they are used for text, as we discussed earlier. Also, don't forget to put them in double or single quotes; Otherwise, javascript will get confused with variable names.
let firstName = 'Akhil';
My convention was to use a single quote, but you can also prefer double-quotes.
Boolean
The boolean data type is essentially a logical type that can only take one of the logical values true
or false
. In other words, a boolean is either true or false. We use boolean values to take decisions with code, as we will see later.
let isProgammer = false;
let javascriptIsFun = true;
console.log(javascriptIsFun); // true
true
These three data types, number, string, and boolean are the most frequently used and essential data types.
Undefined
Undefined is the value taken by the variable that is not yet defined. A variable that's not yet determined is simply a variable that we declared without assigning a value.
let hero; // hero was a defined variable but it has empty value
console.log(hero); // undefined
hero = 'Naruto'; // Assgining a value to variable
console.log(hero); // Naruto
// myFavHero is not a variable yet, it was not defined.
console.log(myFavHero); // error: Uncaught ReferenceError: myFavHero is not defined
Undefined
and not defined
So, we can say that Undefined
means an empty value to an existing variable.
Null
Null is also pretty similar to Undefined because it also means empty value, but it is used in different circumstances. For now, don't worry about the details, I just want you to know that null
also exists.
Symbol
Symbols were introduced in ES2015(ES6), and this data type was not valuable for us; it is used to define a unique value that cannot be changed. And we will talk about this briefly by the end of this course.
BigInt
Starting with ES2020, there is also BigInt
for integers that are too large to be represented by the number type. So, basically, it is another type of number
, and we will talk about this one in another section.
These are the Seven primitive datatypes in javascript, and comparative to other programming languages using types in javascript is a lot easier. It is a lot easier because javascript is based on Dynamic Typing, and we will look into it in the next section.