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 421 of 840
Set a default value for the argument to cover undefined errors while calling a function in JavaScript
In JavaScript, when a function is called without arguments or with undefined values, you can set default parameter values to prevent errors and provide fallback behavior. Basic Default Parameter Syntax The simplest way to set default values is using the ES6 default parameter syntax: function functionName(parameter = defaultValue) { // function body } Example: Simple Default Parameters function greet(name = 'Guest') { console.log(`Hello, ${name}!`); } greet(); // No argument passed greet('Alice'); ...
Read MoreFinding greatest digit by recursion - JavaScript
We are required to write a JavaScript recursive function that takes in a number and returns the greatest digit in the number. For example: If the number is − 45654356 Then the return value should be 6 How It Works The recursive approach extracts digits one by one from right to left using modulo (%) and integer division. Each digit is compared with the current maximum, and the function calls itself with the remaining digits. Example Following is the code − const num = 45654356; const greatestDigit = (num ...
Read MoreDoes JavaScript support block scope?
JavaScript supports block scope for variables declared with let and const, but not for variables declared with var. Understanding this difference is crucial for writing predictable JavaScript code. What is Block Scope? Block scope means a variable is only accessible within the curly braces {} where it was declared. This includes if statements, for loops, while loops, and any other code blocks. Variables with let and const (Block Scoped) Variables declared with let and const are confined to their block: Block Scope with let/const ...
Read MoreHow to generate child keys by parent keys in array JavaScript?
Let's say, we have an array of objects representing a hierarchical structure where each object has an id, parent_id, and title: const arr = [ { id: 1, parent_id: 0, title: 'Movies' }, { id: 2, parent_id: 0, title: 'Music' }, { id: 3, parent_id: 1, title: 'Russian movies' }, { id: 4, parent_id: 2, title: 'Russian music' }, { id: 5, parent_id: 3, title: 'New' }, { id: 6, parent_id: 3, title: 'Top10' ...
Read MoreDigital root sort algorithm JavaScript
Digital root of a positive integer is defined as the sum of all of its digits. We are given an array of integers and need to sort it based on digit root values. If a number comes before b, then the digit root of a should be less than or equal to the digit root of b. If two numbers have the same digit root, the smaller number should come first. For example, 4 and 13 have the same digit root (4 and 1+3=4), but since 4 < 13, the number 4 comes before 13 in the sorted array. ...
Read MoreExtract the value of the to a variable using JavaScript?
To extract the value of the element to a variable using JavaScript, use the textContent property to get the text content from any HTML or SVG text element. Syntax var textValue = document.getElementById("elementId").textContent; Example Extract SVG Text Value This is the JavaScript Tutorial ...
Read MoreIs having the first JavaScript parameter with default value possible?
In JavaScript, you can have default values for the first parameter, but you need special techniques to skip it when calling the function. The most effective approach is using destructuring with spread syntax. The Challenge When you define a function with a default value for the first parameter, you cannot simply omit it in a regular function call: function multiply(firstParam = 10, secondParam) { return firstParam * secondParam; } // This won't work as expected: multiply(5); // firstParam = 5, secondParam = undefined Solution: Using Destructuring with Spread Syntax ...
Read MoreAbsolute sum of array elements - JavaScript
We are required to write a JavaScript function that takes in an array with both positive and negative numbers and returns the absolute sum of all the elements of the array. We are required to do this without taking help of any inbuilt library function. For example: If the array is − const arr = [1, -5, -34, -5, 2, 5, 6]; Then the output should be − 58 Understanding Absolute Sum The absolute sum means we convert all negative numbers to positive and then add all elements. For ...
Read MoreWhat are associative Arrays in JavaScript?
Associative arrays are basically objects in JavaScript where indexes are replaced by user-defined keys. They do not have a length property like normal arrays and cannot be traversed using a normal for loop. What are Associative Arrays? In JavaScript, what we call "associative arrays" are actually objects. Unlike traditional arrays that use numeric indexes, associative arrays use string keys to access values. This makes them perfect for storing key-value pairs where you need meaningful identifiers. Creating Associative Arrays You can create associative arrays using object literal syntax or by assigning properties to an empty object: ...
Read MoreHow can we separate the special characters in JavaScript?
In JavaScript, you can separate special characters from text using the match() method with regular expressions. This technique splits strings into arrays containing both word characters and special characters as separate elements. Syntax arrayName.flatMap(item => item.match(/\w+|\W+/g)); The regular expression /\w+|\W+/g works as follows: \w+ matches one or more word characters (letters, digits, underscore) | acts as OR operator \W+ matches one or more non-word characters (special characters, spaces) g flag ensures global matching throughout the string Example: Separating Special Characters from Names Let's separate special characters from an array of ...
Read More