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 459 of 840
Replace a value if null or undefined in JavaScript?
In JavaScript, you can replace null or undefined values using the logical OR operator (||) or the nullish coalescing operator (??). Syntax // Using logical OR operator var result = value || defaultValue; // Using nullish coalescing operator (ES2020+) var result = value ?? defaultValue; Using Logical OR Operator (||) The || operator returns the first truthy value or the last value if all are falsy: var value = null; console.log("The original value:", value); var actualValue = value || "This is the Correct Value"; console.log("The replaced value:", actualValue); // ...
Read MoreSquare every digit of a number - JavaScript
We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenated. For example: If the number is − 9119 Then the output should be − 811181 because 9² is 81 and 1² is 1. Example Following is the code − const num = 9119; const squared = num => { const numStr = String(num); let res = ''; ...
Read MoreHow to make a fullscreen window with JavaScript?
The Fullscreen API allows you to display HTML elements in fullscreen mode. This is commonly used for videos, images, or interactive content that benefits from maximum screen real estate. Syntax element.requestFullscreen() // Standard element.webkitRequestFullscreen() // WebKit (Safari, Chrome) element.mozRequestFullScreen() // Firefox element.msRequestFullscreen() // IE/Edge Example: Video Fullscreen body { ...
Read MoreHow do JavaScript primitive/object types passed in functions?
In JavaScript, understanding how data types are passed to functions is crucial. Primitive types are passed by value, while objects are passed by reference. This affects how modifications inside functions impact the original data. Pass by Value (Primitives) Primitive types (string, number, boolean, null, undefined, symbol, bigint) are passed by value. A copy is made, so changes inside the function don't affect the original variable. Pass by Value Example function modifyPrimitive(value) { value = value + 10; document.getElementById('output').innerHTML += ...
Read MoreIs it possible to select text boxes with JavaScript?
Yes, it's possible to select text boxes with JavaScript using the select() method. This method highlights all text within an input field, making it ready for user interaction or replacement. The select() Method The select() method programmatically selects all text content in an input field. It's commonly used to improve user experience by pre-selecting text for easy editing or copying. Basic Syntax element.select(); Example: Selecting Text on Button Click Select Text Box Example ...
Read MoreSorting Array with JavaScript reduce function - JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The function should sort the array without using the Array.prototype.sort() method. We are required to use the Array.prototype.reduce() method to sort the array. Let's say the following is our array: const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; Understanding the Approach The reduce() method can be used to build a sorted array by iterating through each element and placing it in the correct position within an accumulator array. We'll use insertion sort logic within the ...
Read MoreHow to draw on scroll using JavaScript and SVG?
Drawing on scroll using JavaScript and SVG creates an engaging animation effect where an SVG path gradually appears as the user scrolls down the page. This technique uses stroke-dasharray and stroke-dashoffset properties to control path visibility. How It Works The technique uses SVG's stroke-dasharray and stroke-dashoffset properties to hide the path initially, then gradually reveals it based on scroll position. The path length is calculated and used to synchronize the drawing with scroll progress. SVG Path Drawing Animation Initial State (Hidden) ...
Read MoreExports & Imports in JavaScript
JavaScript exports and imports enable modular development by allowing us to organize code into reusable modules. This feature helps break down large applications into smaller, maintainable pieces that can be shared across different parts of your codebase. Exports make functions, variables, objects, or any values available outside of a module, while imports bring exported components from other modules into the current module. JavaScript supports two main types of exports: named exports and default exports. Named Exports and Imports Named exports allow you to export multiple components from a single module. Each export must be explicitly named when ...
Read MoreDetermining full house in poker - JavaScript
A "full house" in poker is a hand containing three cards of one rank and two cards of another rank (e.g., three Kings and two Aces). We need to write a JavaScript function that checks if an array of five cards represents a full house. Understanding Full House A full house requires exactly: Three cards of the same rank (three of a kind) Two cards of another same rank (a pair) Example Implementation Here's a JavaScript function to detect a full house: const arr1 = ['K', 'K', 'K', 'A', 'A']; // Full ...
Read MoreHow to get the numbers which can divide all values in an array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array. Let's say the following is our array: const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42]; Understanding the Problem We need to find the common divisors that can divide all numbers in the array. This is essentially finding the Greatest Common Divisor (GCD) of all array elements and then finding all its divisors. Method 1: Finding All Common Divisors ...
Read More