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 440 of 801
JavaScript program to decrement a date by 1 day
JavaScript provides several methods to manipulate dates. To decrement a date by 1 day, we can use the setDate() and getDate() methods together. Syntax date.setDate(date.getDate() - 1); Parameters date.getDate() - Returns the current day of the month (1-31) date.setDate() - Sets the day of the month for the date object Example: Basic Date Decrement Decrement Date by 1 Day body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...
Read MoreHow to determine if date is weekend in JavaScript?
In JavaScript, you can determine if a date falls on a weekend by using the getDay() method. This method returns 0 for Sunday and 6 for Saturday, making weekend detection straightforward. How getDay() Works The getDay() method returns a number representing the day of the week: 0 = Sunday 1 = Monday 2 = Tuesday 3 = Wednesday 4 = Thursday 5 = Friday 6 = Saturday Basic Weekend Check Here's how to check if a specific date is a weekend: var givenDate = new Date("2020-07-18"); var currentDay = givenDate.getDay(); var ...
Read MoreHow to implement asynchronous loop in JavaScript?
Asynchronous loops in JavaScript allow you to perform time-delayed or async operations within loops without blocking the main thread. This is essential when working with APIs, file operations, or timed delays. What is an Asynchronous Loop? A regular loop executes all iterations instantly. An asynchronous loop waits for each iteration to complete before moving to the next, using await with async functions. Using async/await with for Loop Asynchronous Loop ...
Read MoreNo. of ways to empty an array in JavaScript
JavaScript provides several methods to empty an array. Each method has different use cases and behaviors, especially when multiple references to the same array exist. Method 1: Setting to New Array This method creates a new empty array and assigns it to the variable. However, it doesn't affect other references to the original array. Setting to New Array let arr = [1, 2, 3, ...
Read MorePassing parameters to callback functions JavaScript
Callback functions in JavaScript can accept parameters just like regular functions. When passing a callback to another function, you can provide parameters that the callback will use when executed. Basic Callback with Parameters A callback function receives parameters when it's invoked by the calling function: Callback Parameters Run Example function add2(a) { ...
Read MoreHow can I get seconds since epoch in JavaScript?
To get seconds since epoch in JavaScript, you can use Date.getTime() which returns milliseconds since January 1, 1970 (Unix epoch), then divide by 1000 to convert to seconds. Syntax var date = new Date(); var epochSeconds = Math.round(date.getTime() / 1000); Method 1: Using Math.round() The most common approach uses Math.round() to handle decimal precision: var currentDate = new Date(); var epochSeconds = Math.round(currentDate.getTime() / 1000); console.log("Seconds since epoch:", epochSeconds); console.log("Type:", typeof epochSeconds); Seconds since epoch: 1594821507 Type: number Method 2: Using Math.floor() For exact truncation without ...
Read MoreFat arrow functions in JavaScript
Fat arrow functions, introduced in ES6, provide a shorter syntax for writing functions in JavaScript. They use the => operator instead of the function keyword. Syntax // Basic syntax (param1, param2, ...) => { } // Single parameter (parentheses optional) param => { } // No parameters () => { } // Single expression (return implicit) (a, b) => a + b Basic Example Fat Arrow Functions ...
Read MoreRemove elements from array in JavaScript using includes() and splice()?
The includes() method checks whether an array contains a specific element, while splice() is used to add or remove items from an array. Together, they can be used to remove multiple elements from an array efficiently. Syntax array.includes(searchElement) array.splice(start, deleteCount) How It Works The approach involves iterating through the array and using includes() to check if each element should be removed. When a match is found, splice() removes it, and the index is decremented to account for the array shift. Example deleteElementsFromArray = function(elements, ...values) { let elementRemoved ...
Read MoreFat vs concise arrow functions in JavaScript
Arrow functions in JavaScript come in two forms: fat arrow (with curly braces) and concise arrow (without curly braces). The concise form is a streamlined syntax for single-expression functions that provides implicit return functionality. Syntax Comparison Fat arrow function with explicit return: let add = (a, b) => { return a + b; } Concise arrow function with implicit return: let add = (a, b) => a + b; Single parameter (parentheses optional): let square = x => x * x; No parameters (parentheses required): ...
Read MorePseudo mandatory parameters in JavaScript
Pseudo mandatory parameters in JavaScript allow you to enforce that certain function parameters must be provided by the caller. While JavaScript doesn't have built-in mandatory parameters like some other languages, you can simulate this behavior using various techniques. What are Pseudo Mandatory Parameters? Pseudo mandatory parameters are function parameters that appear optional syntactically but will throw an error if not provided. This helps catch bugs early and makes your function's requirements explicit. Method 1: Using a Helper Function The most common approach is to create a helper function that throws an error when called: ...
Read More