Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Data Types

JavaScript Data Types

There are majorly two types of languages. First, one is Statically typed language and Other one is, Dynamically typed language,.

Statically typed language

In this case, each variable and expression type is already known at compile time. Once a variable is declared to be of a certain data type, it cannot hold values of other data types. Example: C, C++, Java.

Java




// Java(Statically typed)
// Variable x is of type int and it
// Will not store any other type.
int x = 5;
  
// Type string and will only accept string values
string y = 'abc';


Dynamically typed languages

These languages can receive different data types over time. For example- Ruby, Python, JavaScript, etc.

Javascript




// JavaScript (Dynamically typed)
// Can store an integer
let x = 5;
  
// Can also store a string.
let name = 'string';


JavaScript is a dynamically typed (also called loosely typed) scripting language. In JavaScript, variables can receive different data types over time. Datatypes are basically types of data that can be used and manipulated in a program. A variable in JavaScript can contain any data. This means that a variable at one time can be a number and at another be a string. 

The latest ECMAScript standard defines eight data types Out of which seven data types are Primitive(predefined) and one complex or Non-Primitive.

Primitive Data Types

The predefined data types provided by JavaScript language are known as primitive data types. Primitive data types are also known as in-built data types.

  • Number: JavaScript numbers are always stored in double-precision 64-bit binary format IEEE 754. Unlike other programming languages, you don’t need int, float, etc to declare different numeric values.
  • String: JavaScript Strings are similar to sentences. They are made up of a list of characters, which is essentially just an “array of characters, like “Hello neveropen” etc.
  • Boolean: Represent a logical entity and can have two values: true or false.
  • Null: This type has only one value that is null.
  • Undefined: A variable that has not been assigned a value is undefined.
  • Symbol: Symbols return unique identifiers that can be used to add unique property keys to an object that won’t collide with keys of any other code that might add to the object.
  • BigInt: BigInt is a built-in object in JavaScript that provides a way to represent whole numbers larger than 253-1.

Non-Primitive Data Types

The data types that are derived from primitive data types of the JavaScript language are known as non-primitive data types. It is also known as derived data types or reference data types.

  • Object: It is the most important data type and forms the building blocks for modern JavaScript. We will learn about these data types in detail in further articles.

JavaScript Primitive Data Types Examples

Number

The number type in JavaScript contains both integer and floating-point numbers. Besides these numbers, we also have some ‘special-numbers’ in javascript that are: ‘Infinity’, ‘-Infinity’, and ‘NaN’. Infinity basically represents the mathematical ‘?’. The ‘NaN’ denotes a computational error.

let num = 2; // Integer 
let num2 = 1.3; // Floating point number
let num3 = Infinity; // Infinity
let num4 = 'something here too'/2; // NaN

String

A String in javascript is basically a series of characters that are surrounded by quotes. There are three types of quotes in Javascript, which are:

let str = "Hello There";
let str2 = 'Single quotes works fine';
let phrase = `can embed ${str}`;

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.

let name = "Mukul";
// embed a variable
alert( `Hello, ${name}!` ); // Hello, Mukul!

Boolean

The boolean type has only two values: true and false. This data type is used to store yes/no values: true means “yes, correct”, and false means “no, incorrect”.

 let isCoding = true; // yes
 let isOld = false;      // no

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:

let age = null;

The ‘null’ data type basically defines a special value that represents ‘nothing’, ’empty’, or ‘value unknown’. Undefined Just like null, Undefined makes its own type. The meaning of undefined is ‘value is not assigned’.

let x;
console.log(x);  // undefined

Symbol

Symbols are new primitive built-in object types introduced as part of ES6. Symbols return unique identifiers that can be used to add unique property keys to an object that won’t collide with keys of any other code that might add to the object. They are used as object properties that cannot be recreated. It basically helps us to enable encapsulation or information hiding.

let symbol1 = Symbol("Geeks")
let symbol2 = Symbol("Geeks")
  
// Each time Symbol() method 
// is used to create new global Symbol
console.log(symbol1 == symbol2); // False

BigInt

BigInt is a built-in object in JavaScript that provides a way to represent whole numbers larger than 253-1. The largest number that JavaScript can reliably represent with the Number primitive is 253-1, which is represented by the MAX_SAFE_INTEGER constant.

var bigBin = BigInt("0b1010101001010101001111111111111111");
// 11430854655n
console.log(bigBin);

JavaScript Non-Primitive Data Types Examples

Object

Objects are not primitive in nature and are a bit complex to understand. Everything in javascript is basically an object, and that is the reason why it becomes very important to have a good understanding of what they are. Objects are used to store keyed collections of various data and more complex entities. We can create objects in multiple ways. One is by making use of figure brackets {…} with an optional list of properties. The properties of an object are in the form of ‘key: value’ pair. Another way is to make use of the ‘new’ keyword. An empty Object can be created using the given below syntax.

let person = new Object(); // "object constructor" syntax
let person = {};  // "object literal" syntax

Both these methods are correct, though it’s totally your call what to choose. We can also put properties inside an Object.

RELATED ARTICLES

Most Popular

Recent Comments