Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to use variable number of arguments to function in JavaScript?
In JavaScript, functions can accept a variable number of arguments using the arguments object or modern rest parameters (...args). The arguments object contains all arguments passed to a function, regardless of how many parameters are defined. Using the arguments Object The arguments object is an array-like object that contains all arguments passed to a function: function functionArgument(val1, val2, val3) { var res = ...
Read MoreHow to Scroll to the top of the page using JavaScript/ jQuery?
Scrolling to the top of a page is a common user experience feature that improves navigation, especially on long pages. JavaScript and jQuery provide several methods to achieve smooth scrolling to the top. Using jQuery animate() Method The most popular approach uses jQuery's animate() method to create a smooth scrolling effect: body { height: 2000px; padding: 20px; } #scrollBtn { position: fixed; bottom: 20px; right: 20px; padding: ...
Read MoreHow can I format numbers as dollars currency string in JavaScript?
This tutorial teaches us to format numbers as a dollar currency string in JavaScript. Now, the question arises that why we need to format the numbers as a dollar currency string? Well, the answer is here. Users can think about the scenario where they are developing an eCommerce website or application and must show product prices to the customers. What if they render product prices like 2500 and $2500? The second one looks better as '$' represents the USD currency and is the standardized way to show off the USD currency. Also, second approach is more understandable. Below, we ...
Read MoreWhat is the JavaScript version of sleep()?
JavaScript doesn't have a built-in sleep() function like other languages, but you can create one using Promise and setTimeout() with async/await. Creating a Sleep Function The most common approach is to create a promise-based sleep function that works with async/await: function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function displayMessages() { document.write('Wait for 3 seconds!'); await sleep(3000); document.write('After 3 seconds!'); document.write('Wait for 2 ...
Read MoreWhy do we use a plus sign in front of function name in JavaScript?
The +function() {} notation is primarily used to force the parser to treat whatever follows the + as an expression. This is commonly used for Immediately Invoked Function Expressions (IIFEs). The Problem Without + Without the plus sign, JavaScript interprets a function declaration, which cannot be immediately invoked: // This causes a syntax error function() { console.log("Demo!"); }(); Using + to Create an IIFE The + operator converts the function declaration into an expression, allowing immediate invocation: IIFE with Plus Sign ...
Read MoreWhat is the difference between getter and setter in JavaScript?
In JavaScript, getters and setters are special methods that allow you to define how properties are accessed and modified in objects. They provide a way to control property access while maintaining a clean interface. Getter A getter is a method that gets executed when a property is accessed. It uses the get keyword and allows you to define custom logic for retrieving a property value. The getter appears as a regular property but internally calls a function. Setter A setter is a method that gets executed when a property is assigned a value. It uses the ...
Read MoreHow to delete a setter using the delete operator in JavaScript?
To delete a setter using the delete operator in JavaScript, you use the delete keyword followed by the property name. This removes both the getter and setter methods associated with that property. Syntax delete obj.propertyName Example: Deleting a Setter Here's how to delete a setter along with its corresponding getter: var department = { deptName: "Marketing", deptZone: "North", deptID: 101, get details() { return ...
Read MoreHow to delete a getter using the delete operator in JavaScript?
In this tutorial, we are going to learn how we can delete a getter function using the delete operator in JavaScript. Getter functions are used to get the property of an object and bind the property with the getter function i.e., whenever the property is called the getter function will also be called with it. You can only have one getter or setter per name on an object as we cannot create more than two getters using the same name in JavaScript. To delete a getter function in JavaScript we use the delete operator which uses the keyword "delete". ...
Read MoreHow to define getter and setter functions in JavaScript?
JavaScript getter and setter functions allow you to define how properties are accessed and modified in objects. They provide a way to execute custom logic when getting or setting property values. Getter Functions A getter function is called automatically when a property is accessed. It uses the get keyword and allows you to compute or format values dynamically. Setter Functions A setter function is called automatically when a property is assigned a value. It uses the set keyword and receives the new value as a parameter, allowing you to validate or process the data before storing ...
Read MoreWhat are the latest operators added to JavaScript?
JavaScript has introduced several powerful operators in recent versions, with the rest and spread operators being among the most significant additions in ES6 (ES2015). These operators use the same three-dot syntax (...) but serve different purposes depending on context. Rest Operator The rest operator allows you to represent an indefinite number of arguments as an array. It's indicated by three dots (...) and precedes a parameter in function definitions. Example Here's how to use the rest operator to handle multiple function arguments: ...
Read More