Introduction to JavaScript Data Types With Examples
JavaScript supports several data types including numbers, strings, booleans, arrays, objects, null, and undefined. Numbers represent numeric values, strings represent text, booleans represent true or false values, arrays store multiple values in an ordered list, objects store key-value pairs, null represents the intentional absence of any object value, and undefined represents the absence of a defined value. Let’s understand!
JavaScript is a powerful programming language used to make web pages interactive. It is a text-based scripting language that includes data types to identify the data stored in a variable during execution. JavaScript is a dynamically typed language that does not require you to define the data type before declaring a variable. In JavaScript, a variable can be assigned any value. In this blog, you will learn about the most common JavaScript data types with the help of examples.
Want to be a certified JavaScript developer? Explore Online JavaScript Courses
Table of Content
- What are Data Types in JavaScript?
- JavaScript Data Types- Primitive
- JavaScript Data Types- Non- Primitive
- JavaScript Data Types – Summary
What are Data Types in JavaScript?
Data types in JavaScript define the data type that a variable can store. JavaScript includes primitive and non-primitive data types. The primitive data types in JavaScript include string, number, boolean, undefined, null, and symbol. The non-primitive data type includes the object. A variable of primitive data type can contain only a single value.
Best-suited JavaScript courses for you
Learn JavaScript with these high-rated online courses
JavaScript Data Types
Primitive Data Types
These are the lowest level of data value in the JavaScript programming language. They define immutable values – which can’t be changed.
Explore– Popular Programming Courses
String
A string data type is a group of characters or textual content enclosed by single quotes (‘ ‘) or double-quotes (” “), or tick signs (` `).
For Example
var str1 = 'Hello, World!'; // Output: Hello, World! <p>var str2 = "His name is ‘John’"; // Output: His name is ‘John’</p>
Also Read: What is Coding: Difference Between Coding And Programming
Number
A number data type is a numeric value. It can be an integer (positive or negative), floating-point, or exponential.
For Example
var a=47; // positive integer valuevar b=629.5; // floating pointvar c=-5; // number with negative value
The number data type also includes special values such as Infinity, -Infinity, and Nan (not a number).
Infinity – when a positive number is divided by zero, the result is Infinity
-Infinity – when a negative number is divided by zero, the result is -Infinity
For Example
var x = 25;var y = -25;var z = 0;alert(x/z); // returns Infinityalert(y/z); // returns -Infinity
Nan – when we perform any operation between a number and a non-numeric value, the result is Nan.
For Example
var x = ‘hello’;var y = 100;alert(x*y); // Nan
Boolean
Boolean is a logical data type used to compare two variables or check a condition. It has only two values, namely, true and false.
For Example – Comparing two variables
var a =10;var b=9;a==b // returns false
For Example – Checking a condition
If(x<y){alert(x is a smaller number than y);}
Undefined
An undefined data type means that a variable is declared and not assigned any value. It is the default value of a variable that has not been defined.
For Example
var x; // undefinedconsole.log(x); // returns undefined
Null
A null value means no value. It means an empty value or absence of value.
For Example
var x = null;console.log(x); // returns null
Symbol
The symbol data type in JavaScript defines a property of an object which is private to the object. The value with the Symbol data type can be referred to as a symbol value. The symbol refers to the ‘key’ of the key-value pair of an object. Every symbol is unique. Two symbols with the same key values are not the same.
For Example
let value1 = Symbol("value");let value2 = Symbol("value");alert(value1 == value2); // false
In the above example, both value1 and value2 contain the same key values but they are different as they are of the symbol type.
Non-Primitive Data Type
Object
An object data type stores multiple values or data collection regarding properties and methods. It contains key-value pairs in its address. The properties of the object are written as key: value pairs. Each pair is separated by commas, enclosed in curly braces {}. The key must be a string, and the value can be of any data type.
For Example
var student = {firstName: 'john', class: 7};
Read more: Working with JavaScript Objects
JavaScript Data Types – Summary
Below is a summary of the 7 most common data types in JavaScript
Data Types | Description | Example |
String | It contains a group of characters | ‘hello’, “hello, world!” |
Number | Includes an integer or a floating-point number, or an exponential value | 5, -56, 2567.54 |
Boolean | Contains any of two values – true or false | True and false |
undefined | It denotes that a variable is just declared and is not assigned any value | let a; |
null | Denotes no value | let x = null; |
Symbol | or an exponential value | let value1 = Symbol(‘val’); |
Object | key-value pairs of collection of data | let emp = { }; |
Also Read: Top JavaScript Interview Questions and Answers
Conclusion
In this article, we explained different data types in JavaScript with examples. We hope that this blog equipped you with a better understanding of the topic and prepared you to move on to creating efficient JavaScript programs.
FAQs
What are the different data types in JavaScript?
JavaScript has several data types, including numbers, strings, booleans, arrays, objects, null, and undefined.
How do I check the data type of a variable in JavaScript?
You can use the "typeof" operator followed by the variable name to determine its data type. For example, "typeof myVariable" will return a string indicating the data type.
Can JavaScript variables change their data type?
Yes, JavaScript variables are dynamically typed, which means they can hold different data types at different times. A variable can be assigned a number and later assigned a string or any other data type.
What is the difference between null and undefined in JavaScript?
"null" is a value that represents the intentional absence of any object value, while "undefined" is a value that indicates the absence of a defined value. "null" is assigned by developers, whereas "undefined" is the default value for variables that have been declared but not assigned a value.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio