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 by AmitDiwan
Page 476 of 840
Getting tomorrow and day after tomorrow date in JavaScript
Using the Date class in JavaScript, we can easily calculate tomorrow and the day after tomorrow from the current date. The new Date() constructor returns a JavaScript Date object for the current day. Getting Today's Date First, let's get today's date using the Date constructor: const today = new Date(); console.log("Today:", today.toDateString()); Today: Thu Aug 13 2020 Method 1: Using setDate() with Date Copying We can create new Date objects by copying today's date and then adding days using the setDate() method: // Getting today's date const today ...
Read MoreReplace a letter with its alphabet position JavaScript
We are required to write a function that takes in a string, trims it off any whitespaces, converts it to lowercase and returns an array of numbers describing corresponding characters positions in the english alphabets, any whitespace or special character within the string should be ignored. Problem Example Input → 'Hello world!' Output → [8, 5, 12, 12, 15, 23, 15, 18, 12, 4] How It Works The algorithm processes each character by: Converting to lowercase to handle both cases uniformly Getting ASCII code using charCodeAt() ...
Read MoreCan we have a return statement in a JavaScript switch statement?
Yes, you can use return statements in a JavaScript switch statement, but only when the switch is inside a function. The return statement will immediately exit the function with the specified value, making break statements unnecessary. How Return Statements Work in Switch When a return statement is executed in a switch case, it immediately exits the function and returns the value. This eliminates the need for break statements since the function execution stops. Example: Day Name Function Return ...
Read MoreRemove duplicates from a array of objects JavaScript
Removing duplicates from an array of objects is a common task in JavaScript. We need to identify objects with the same properties and values, keeping only unique objects in the result. Using JSON.stringify() with Map The most straightforward approach uses JSON.stringify() to convert objects to strings for comparison: const arr = [ { "timestamp": 564328370007, "message": "It will rain today" }, { ...
Read MoreHow to use JavaScript map() method to access nested objects?
The map() method can be used to iterate through arrays of nested objects and safely access their properties using conditional checks. Understanding Nested Objects When working with arrays containing objects that have different structures, some objects may have nested properties while others don't. Here's our sample data: var details = [ { id: "101", firstName: "John", lastName: "Smith", age: 25, ...
Read MoreHow to add a character to the beginning of every word in a string in JavaScript?
We need to write a function that takes two strings and returns a new string with the second argument prepended to every word of the first string. For example: Input → 'hello stranger, how are you', '@@' Output → '@@hello @@stranger, @@how @@are @@you' If the second argument is not provided, we'll use '#' as the default character. Solution Using split() and map() The most straightforward approach is to split the string into words, prepend the character to each word, and join them back: const str = 'hello stranger, how are ...
Read MoreHow to convert comma separated text in div into separate lines with JavaScript?
Converting comma-separated text in a div into separate lines is a common requirement in web development. This can be achieved using JavaScript's string manipulation methods along with DOM manipulation. Let's say we have the following comma-separated text in a div: This, is, the, first, JavaScript, program To convert comma-separated text into separate lines, we need to use trim() along with split() based on the comma separator. Method 1: Converting to List Items This approach splits the comma-separated text and converts each item into a list element: ...
Read MoreProgram to append two given strings such that, if the concatenation creates a double character then omit one of the characters - JavaScript
We are required to write a JavaScript function that takes in two strings and concatenates the second string to the first string. If the last character of the first string and the first character of the second string are the same then we have to omit one of those characters. Let's say the following are our strings in JavaScript − Problem Example const str1 = 'Food'; const str2 = 'dog'; // Expected output: 'Foodog' (last 'd' of 'Food' matches first 'd' of 'dog') Solution Let's write the code for this function − ...
Read MoreNamed arguments in JavaScript.
Named arguments in JavaScript allow you to pass parameters to functions using an object, making function calls more readable and flexible. This technique uses object destructuring to extract named properties from the passed argument. Syntax function functionName({ param1, param2, param3 }) { // Function body } // Call with named arguments functionName({ param1: value1, param2: value2, param3: value3 }); Basic Example Named Arguments Example Named ...
Read MoreNesting template strings in JavaScript
Nesting template strings in JavaScript allows you to embed one template literal inside another. This is useful when building complex strings with dynamic content or when passing template literals as function arguments. What are Nested Template Strings? Nested template strings occur when you place one template literal (using backticks) inside another. The inner template string is evaluated first, then the outer one. Basic Syntax `Outer template ${`Inner template ${variable}`} continues here` Simple Example Nested ...
Read More