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
Web Development Articles
Page 530 of 801
How do you find out the caller function in JavaScript?
In this tutorial, we will learn to find out the caller function in JavaScript. The function is the reusable code, and users can call it from anywhere. But sometimes, they need to know who is the caller function to perform some operation. For example, suppose that we can call any single function from another 2 to 3 functions, and we need to perform some operation according to the caller function. Users can understand this scenario by the below code example. function func3() { if (caller is func2()) { ...
Read MoreHow do I declare a namespace in JavaScript?
A namespace is a programming concept that gives identifiers (names of types, functions, variables, etc.) scope to avoid name conflicts. JavaScript doesn't have built-in namespace support, but we can simulate namespaces using objects to organize code and prevent variable name collisions. In modern web applications that use multiple libraries and components, namespaces help avoid confusion and conflicts in code. This tutorial covers how to implement namespaces in JavaScript using different approaches. Object Literal Notation The most common way to create namespaces in JavaScript is using Object Literal Notation. This approach creates a global object that contains all ...
Read MoreWhat does the exclamation mark do before the function in JavaScript?
The exclamation mark (!) before a function in JavaScript is used to create an Immediately Invoked Function Expression (IIFE). It transforms a function declaration into an expression that can be executed immediately. How It Works The ! operator has lower precedence than the parentheses (), so the function executes first, then the ! operator applies to its return value: !function() { console.log("IIFE executed!"); }(); IIFE executed! Return Value Behavior Since functions return undefined by default, the ! operator converts this to true: let result1 ...
Read MoreHow to add number of days to JavaScript Date?
In this tutorial, we will learn how to add a number of days to a JavaScript Date object. Here we will discuss two methods which are following. Using the setDate( ) Method Using the getTime() Method Using the setDate( ) Method JavaScript date setDate() method sets the day of the month for a specified date according to local time. Syntax Date.setDate(dayValue) Here dayValue is an integer from 1 to 31, representing the day of the month. Approach To add a number of ...
Read MoreHow 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 More