- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - void Keyword
The void keyword in JavaScript is used as an operator that evaluates a given expression and returns undefined. The void is an important keyword in JavaScript. The meaning of the void is null or empty.
The void keyword can be used as a unary operator that appears before its single operand, which may be of any type. This operator specifies an expression to be evaluated without returning a value or returning the undefined.
Syntax
The syntax to use the void keyword in JavaScript −
void operand;
In the above syntax, the operand can be any expression.
Return Value
It returns the undefined.
Example
In the below code, we used the 10 with the 'void' keyword. In the output, you can observe that it returns undefined.
<html> <body> <div id = "output">The value of the ans variable is: </div> <script> let ans = void 10; document.getElementById("output").innerHTML += ans; </script> </body> </html>
Output
The value of the ans variable is: undefined
Importance of Precedence of void Keyword
Generally, the JavaScript 'void' keyword is used to return the primitive undefined value, but if you don't take precedence in the consideration, it can return a different value.
Let's understand it via the example below.
Example
In the below code, the void operator takes precedence over the strict equality operator in the first expression. So, it first evaluates 'void 10' to undefined and compares it with 20. So, it returns false.
The second expression evaluates '10 === 20' first to false and evaluates 'void false' to undefined.
<html> <body> <div id = "output1"> </div> <div id = "output2"> </div> <script> let res1 = void 10 === 20; let res2 = void (10 === 20); document.getElementById("output1").innerHTML += res1; document.getElementById("output2").innerHTML += res2; </script> </body> </html>
Output
false undefined
What is javascript:void(0)?
Let's break down the javascript:void(0) into two parts and understand both separately.
javascript:
You can use the 'javascript:' to create a pseudo URL. Using 'javascript:' you can create the interactive URL.
You need to write the expression after 'javascript:', and when users click the anchor text, it executes the JavaScript code.
Let's understand it via the example below.
Example
In the below code, we have created the link text and used the JavaScript URL as a href. When you click the anchor text, it writes it in the HTML document.
<html> <body> <a href = "javascript:document.write('Anchor text is clicked!')"> Click me! </a> </body> </html>
In this way, you can use the 'javascript:uri' to create the pseudo URL.
void(0)
The void(0) evaluates the JavaScript expression, but it returns undefined. So, when you want to execute the expression without performing any action, you can use the void(0).
javascript: void(0)
When you don't want to navigate users to another web page when they click on the link, you can use the 'javascript:void(0)' as a href value of the anchor tag.
Let's understand it via the example below.
Example
It won't do anything Whenever you click the anchor text in the code below.
<html> <body> <a href = "javascript:void(0)"> Click me! </a> </body> </html>
You can also use the 'javascript:void(0)' when you want to update the web page using the URI but don't want to navigate to another web page.
Example
In the below code, when you click the anchor text, it will change the background color of the body, rather than navigating to another web page.
<html> <body> <a href = "javascript:void(0);" onclick = "document.body.style.backgroundColor = 'blue'"> Change Background Color </a> </body> </html>
The void Keyword with Functions
When you use the void keyword with JavaScript functions, it returns undefined. After that, if you try to execute the function, it will throw an error, as the 'void' operator considers the function as its operand and evaluates it as undefined.
Example
In the below code, we have defined the test() function and used the 'void' keyword with it.
Also, used the try…catch block to catch the error.
In the try block, we execute the function, and in the output, you can observe that control goes into the catch block.
<html> <body> <div id = "demo"> </div> <script> const output = document.getElementById('demo'); try { void function test() { output.innerHTML += "The test function is executed!"; } test(); } catch (error) { output.innerHTML += "The test function is undefined!"; } </script> </body> </html>
Output
The test function is undefined!
The void Keyword with Immediately Invoked Function
When you use the 'void' keyword with the JavaScript immediately invoked function, it invokes the function expression first and evaluates it as undefined.
Example
In the below code, we have defined the immediately invoked function with the void keyword. You can observe that it invokes the function first and returns undefined.
<html> <body> <div id = "demo"> </div> <script> const output = document.getElementById("demo"); void function () { output.innerHTML = "Unknown function is executed!"; }(); </script> </body> </html>
Output
Unknown function is executed!
Also, you can use the 'void' keyword with the arrow function to return undefined from the arrow function. The JavaScript:void() URI can also be used to prevent the web page reloading by default, like the preventDefault() method.