Updated February 21, 2023
Overview of JavaScript Data Types
While working with the computer, we found that many data types exist. Like number, String, Boolean, etc. But what’s the role of this data in a programming language? Of course, any programming language has predefined data types. For example, JavaScript also has its own set of data types.
What is JavaScript?
JavaScript is a client-side scripting language. Java and JavaScript are two different programming languages; they are not similar. Before learning JavaScript, we recommend you to learn HTML and CSS first, as those are essential to learning web development technologies. JavaScript runs on the client-side of the browser. JavaScript is designed in a way to run on a browser. JavaScript is most important in front-end web development, making our web pages dynamic.
What does a dynamic webpage mean?
HTML is a structured language. It is static in nature. Everything you portray on the HTML shows the same output in the web browser. There was a need to get some events to happen on the client side. Like if the user clicks on a button that needs to happen something, make validation of forms to send correct data over the network. JavaScript is used to make web pages more interactive. It is possible to make web pages more user-friendly and easy to use. By making form validations and some events, we are indirectly reducing the load on the server, which helps the server to serve even faster to the request.
Declaring Variable in JavaScript
In JavaScript, we are declaring a variable as follows:
var a =10;
Look at the above code carefully. If you came from other programming languages like Java, you would see that we are not mentioning the type of variable we declare. And once the value is assigned to the variable, it cannot be changed.
In Java, you may do the following while declaring the variable.
int a = 10;
//you cannot reassign value to the same variable
int a = 20; // this is not allowed in strictly typed languages.
While declaring a variable, we tell the compiler that this variable is of type integer, But In JavaScript, the case is different. JavaScript is a dynamically typed language. That means you write keyword var and then the variable name. After that, the JavaScript engine internally changes the variable’s value to the correct data type.
Also, In JavaScript, we can reassign value to the variable even if we assigned t=value earlier to the same variable.
These features of JavaScript make it somewhat confusing to understand. But if you look closely, it’s very easy.
Data Types in Javascript
Two data types exist in javascript. one is primitive, and the other is user-defined.
There are a total of five data types in JavaScript as below:
1. Number
The number is a primitive data type in JavaScript. In JavaScript for numbers, only one data type is there. Unlike other languages, JavaScript has no concept like float, decimal, or double.
Ex.
var n = 10;
var m = 10.20;
Above both variables number, even if variable m has a decimal point.
Both variables are addressed as numbers only.
2. String
The string is a primitive data type in JavaScript. The string is nothing but a collection of characters. Or some words. Look at the following example.
var str1 = "Hello World";
var str1 has value “Hello world”
you can write string value in single or double-quotes.
Ex:
var str2 = "Hello World";
and
var str3 = 'Hello world';
both are the same.
Sometimes, javascript doesn’t understand the quotes as they are nested. That time we are using the escape character to work it.
Let’s look at the following example. This shows we can use a single quote in double quotes and vice/Versa.
var str1 = "it's an ice cream parlor";
We need to use quotes carefully.
3. Boolean
Boolean is a primitive data type in JavaScript. Boolean has two values, true and false. In JavaScript, we use Boolean values when there is a condition we want to apply. This is very useful when checking the condition while validating something with JavaScript.
Ex:
var isvalide = false;
Ex:
var x = 10;
var y = 10;
x == y //this will give true.
Boolean is useful when we check whether the condition is true or false.
4. Undefined
Undefined is a primitive data type in JavaScript. Any variable in javascript which not have value. This means it is declared but not assigned with any value that variable has by default value undefined in JavaScript. A variable which not been assigned a value to the variable.
Ex:
var a;
We will get the undefined value if we save this and open this in the browser console.
Write the following code in the console.
Console.log(typeof a);
This will give undefined as an output.
5. Null
Null is also a primitive data type in JavaScript. If we want a particular value intentionally empty, we can assign it the Null value. For example, we can say when some condition is we want to take value from the user at run time; then we can use Null data type.
Ex:
var a = null;
Non-Primitive Data Types of JavaScript
Object, Array is the non-primitive data type. Finally, a symbol is a new data type introduced in ECMAScript.
Object
Object is the data type all the JavaScript is about.
We can define the object with object literal syntax. An object contains a pair of key-value pairs. Look at the following example.
var person = {
first_name: "abc",
last_name: "xyz",
age:25
}
An object contains multiple values.
Arrays
Arrays are containers to store multiple values in a single variable.
Var book =["abc,"" xyz"," pqr"];
In JavaScript array is referred to as an object. So, for example, if we type the following code in the browser console, we can get a type of Array as an object.
Console.log(typeof book);
Conclusion
JavaScript data types are very important to understand. JavaScript is a loosely typed language. JavaScript intelligent engine covers the assigned value to the variable in an appropriate type. Therefore, while learning JavaScript, you should know data types.
Recommended Articles
This has been a guide to JavaScript Data Types. Here we have discussed the Overview of JavaScript Data Types and the Concept of Data Types. You may also look at the following article to learn more –