In JavaScript, each value has a data type, defining its nature (e.g., Number, String, Boolean) and operations. Data types are categorized into Primitive (e.g., String, Number) and Non-Primitive (e.g., Objects, Arrays).
Primitive Data Type
1. Number
The Number data type in JavaScript includes both integers and floating-point numbers. Special values like Infinity, -Infinity, and NaN represent infinite values and computational errors, respectively.
JavaScript let n1 = 2; console.log(n1) let n2 = 1.3; console.log(n2) let n3 = Infinity; console.log(n3) let n4 = 'something here too' / 2; console.log(n4)
2. String
A String in JavaScript is a series of characters that are surrounded by quotes. There are three types of quotes in JavaScript, which are.
JavaScript let s1 = "Hello There"; console.log(s1); let s2 = 'Single quotes work fine'; console.log(s2); let s3 = `can embed ${s1}`; console.log(s3);
OutputHello There Single quotes work fine can embed Hello There
There’s no difference between ‘single’ and “double” quotes in JavaScript. Backticks provide extra functionality as with their help of them we can embed variables inside them.
3. Boolean
The boolean type has only two values i.e. true and false.
JavaScript let b1 = true; console.log(b1); let b2 = false; console.log(b2);
4. Null
The special null value does not belong to any of the default data types. It forms a separate type of its own which contains only the null value.
JavaScript let age = null; console.log(age)
The ‘null’ data type defines a special value that represents nothing, or empty value.
5. Undefined
A variable that has been declared but not initialized with a value is automatically assigned the undefined value. It means the variable exists, but it has no value assigned to it.
JavaScript
6. Symbol (Introduced in ES6)
Symbols, introduced in ES6, are unique and immutable primitive values used as identifiers for object properties. They help create unique keys in objects, preventing conflicts with other properties.
JavaScript let s1 = Symbol("Geeks"); let s2 = Symbol("Geeks"); console.log(s1 == s2);
7. BigInt (Introduced in ES2020)
BigInt is a built-in object that provides a way to represent whole numbers greater than 253. The largest number that JavaScript can reliably represent with the Number primitive is 253, which is represented by the MAX_SAFE_INTEGER constant.
JavaScript let b = BigInt("0b1010101001010101001111111111111111"); console.log(b);
Non-Primitive Data Types
The data types that are derived from primitive data types are known as non-primitive data types. It is also known as derived data types or reference data types.
1. Object
JavaScript objects are key-value pairs used to store data, created with {} or the new keyword. They are fundamental as nearly everything in JavaScript is an object.
JavaScript let gfg = { type: "Company", location: "Noida" } console.log(gfg.type)
2. Arrays
An Array is a special kind of object used to store an ordered collection of values, which can be of any data type.
JavaScript let a1 = [1, 2, 3, 4, 5]; console.log(a1); let a2 = [1, "two", { name: "Object" }, [3, 4, 5]]; console.log(a2);
Output[ 1, 2, 3, 4, 5 ] [ 1, 'two', { name: 'Object' }, [ 3, 4, 5 ] ]
3. Function
A function in JavaScript is a block of reusable code designed to perform a specific task when called.
JavaScript // Defining a function to greet a user function greet(name) { return "Hello, " + name + "!"; } // Calling the function console.log(greet("Ajay"));
4. Date Object
The Date object in JavaScript is used to work with dates and times, allowing for date creation, manipulation, and formatting.
JavaScript // Creating a new Date object for the current date and time let currentDate = new Date(); // Displaying the current date and time console.log(currentDate);
Output2025-03-08T06:23:33.202Z
5. Regular Expression
A RegExp (Regular Expression) in JavaScript is an object used to define search patterns for matching text in strings.
JavaScript // Creating a regular expression to match the word "hello" let pattern = /hello/; // Testing the pattern against a string let result = pattern.test("Hello, world!"); // Returns true because "Hello" matches the pattern console.log(result);
Interesting Facts about Data Types
1. Dynamically Typed : JavaScript Variables are not bound to a specific data type. Mainly data type is stored with value (not with variable name) and is decided & checked at run time.
JavaScript let x = 42; console.log(x) x = "hello"; console.log(x) x = [1, 2, 3] console.log(x)
Output42 hello [ 1, 2, 3 ]
2. Everything is an Object (Sort of): In JavaScript, Functions are objects, arrays are objects, and even primitive values can behave like objects temporarily when you try to access properties on them.
JavaScript let s = "hello"; console.log(s.length); // Example with a number let x = 42; console.log(x.toString()); // Example with a boolean let y = true; console.log(y.toString()); /* Internal Working of primitives to be treeated as objects // Temporary wrapper object let temp = new String("hello"); console.log(temp.length); // 5 // The wrapper is discarded after use temp = null; */
3. NaN is not equal to itself: NaN Stands for “Not-a-Number”, It is used to represent a computational error. NaN is technically of type number.
JavaScript console.log(typeof NaN); console.log(NaN === NaN);
4. A Symbol is Never Equal to Another One : Symbol
is a unique and immutable data type often used for creating private properties and methods. Symbols are never equal to any other Symbol.
JavaScript let s1 = Symbol("abc"); let s2 = Symbol("abc"); console.log(s1 === s2);
5. Undefined and Null: undefined represents a variable that has been declared but not assigned, while null is an explicit assignment representing “no value”.
6. Integers are Floating are Numbers only. There is only one type number that covers both integers and floating point numbers.
JavaScript let x = 42; // Integer let y = 42.5; // Floating-point console.log(typeof x); console.log(typeof y);
7. A character is also a string. There is no separate type for characters. A single character is also a string.
JavaScript let s1 = "gfg"; // String let s2 = 'g'; // Character console.log(typeof s1); console.log(typeof s2);
Similar Reads
Introduction to JavaScript Course - Learn how to build a task tracker using JavaScript
This is an introductory course about JavaScript that will help you learn about the basics of javascript, to begin with, the dynamic part of web development. You will learn the basics like understanding code structures, loops, objects, etc. What this course is about? In this course we will teach you
4 min read
JavaScript Course What is JavaScript ?
JavaScript is a very famous programming language that was originally started in the year of 1995 with the motive of making web pages alive. It is also an essential part of the web developer skillset. In simple terms, if you want to learn web development then learn HTML & CSS before starting JavaScri
3 min read
JavaScript Hello World
The JavaScript Hello World program is a simple tradition used by programmers to learn the new syntax of a programming language. It involves displaying the text "Hello, World!" on the screen. This basic exercise helps you understand how to output text and run simple scripts in a new programming envir
2 min read
JavaScript Course Understanding Code Structure in JavaScript
Inserting JavaScript into a webpage is much like inserting any other HTML content. The tags used to add JavaScript in HTML are <script> and </script>. The code surrounded by the <script> and </script> tags is called a script blog. The 'type' attribute was the most important a
3 min read
JavaScript Course Variables in JavaScript
Variables in JavaScript are containers that hold reusable data. It is the basic unit of storage in a program. The value stored in a variable can be changed during program execution. A variable is only a name given to a memory location, all the operations done on the variable effects that memory loca
4 min read
JavaScript Data Types
In JavaScript, each value has a data type, defining its nature (e.g., Number, String, Boolean) and operations. Data types are categorized into Primitive (e.g., String, Number) and Non-Primitive (e.g., Objects, Arrays). Primitive Data Type1. NumberThe Number data type in JavaScript includes both inte
6 min read
JavaScript Course Operators in JavaScript
An operator is capable of manipulating a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript, operators are used for comparing values, performing arithm
7 min read
JavaScript Course Interaction With User
Javascript allows us the privilege to which we can interact with the user and respond accordingly. It includes several user-interface functions which help in the interaction. Let's take a look at them one by one. JavaScript Window alert() Method : It simply creates an alert box that may or may not h
2 min read
JavaScript Course Logical Operators in JavaScript
logical operator is mostly used to make decisions based on conditions specified for the statements. It can also be used to manipulate a boolean or set termination conditions for loops. There are three types of logical operators in Javascript: !(NOT): Converts operator to boolean and returns flipped
3 min read
JavaScript Course Conditional Operator in JavaScript
JavaScript Conditional Operators allow us to perform different types of actions according to different conditions. We make use of the 'if' statement. if(expression){ do this; } The above argument named 'expression' is basically a condition that we pass into the 'if' and if it returns 'true' then the
3 min read