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 on Trending Technologies
Technical articles with clear explanations and examples
Conditional statements in JavaScript
Conditional statements allow you to execute different blocks of code based on specific conditions. JavaScript provides three main types of conditional statements to control program flow. Types of Conditional Statements if statement − Executes code only if a specific condition is true. if...else statement − Checks one condition and executes different code blocks for true and false cases. if...else if...else statement − Handles multiple conditions by checking them sequentially. Syntax // if statement if (condition) { // code to execute ...
Read MoreJavaScript reduce sum array with undefined values
If you're working with arrays in JavaScript, you may encounter situations where your array contains undefined values. This can happen when you're processing data from various sources or working with incomplete datasets. One common problem developers face is summing the values of an array with undefined elements. JavaScript's reduce() method is a versatile tool that can help you solve this problem efficiently. What is the reduce() Method in JavaScript? The reduce() method in JavaScript is a powerful tool that allows you to iterate over an array and accumulate a single result. It takes a callback function as its ...
Read MoreReduce sum of digits recursively down to a one-digit number JavaScript
We have to write a function that takes in a number and keeps adding its digits until the result is a one-digit number. When we have a one-digit number, we return it. The code uses a recursive function that keeps adding digits until the number is between -9 and 9. We handle the sign separately to avoid duplicating logic. How It Works The algorithm follows these steps: Take the absolute value of the number to handle sign separately If the number is greater than 9, split it into digits and sum them Recursively call the ...
Read MoreChanging value of nested object keys in JavaScript
In JavaScript, you can change nested object values using dot notation and square brackets. This is useful when working with complex data structures containing multiple levels of nesting. Syntax // Using dot notation object.property.nestedProperty = newValue; // Using square brackets object['property']['nestedProperty'] = newValue; // Mixed approach object.property['nestedProperty'] = newValue; Example: Changing Nested Object Values var details = { "customer": { "customerDetails": { "otherDetails": [ ...
Read MoreHow to write a Regular Expression in JavaScript to remove spaces?
To remove spaces from strings in JavaScript, regular expressions provide a powerful and flexible solution. The most common approach uses the /\s/g pattern with the replace() method. Basic Syntax string.replace(/\s/g, '') Where: \s - matches any whitespace character (spaces, tabs, newlines) g - global flag to replace all occurrences '' - empty string replacement Example: Removing All Spaces var str = "Welcome to Tutorialspoint"; document.write("Original: " + str); // Removing all spaces var result = str.replace(/\s/g, ''); document.write("After removing spaces: " + result); ...
Read MoreHow to work with document.links in JavaScript?
In JavaScript, the document.links property provides access to all anchor () and area () elements that have an href attribute. This DOM property returns a live HTMLCollection of clickable links in the document. Syntax let links = document.links; Properties The document.links collection provides one main property: length − Returns the number of links in the collection. Working with document.links Properties The example below demonstrates how to access link properties. Note that only elements with href attributes are included in the collection. Working ...
Read MoreHow to use JavaScript Object.defineProperty?
The Object.defineProperty() method allows you to define a new property or modify an existing property on an object with precise control over its behavior. Syntax Object.defineProperty(obj, prop, descriptor) Parameters obj – The object on which to define the property prop – The name of the property to be defined or modified descriptor – An object that describes the property's attributes Property Descriptor Attributes The descriptor object can contain the following attributes: value – The value of ...
Read MoreDOMException Failed to load because no supported source was found
The "DOMException Failed to load because no supported source was found" error occurs when media elements (video, audio) cannot load their source files. This is commonly caused by CORS restrictions, incorrect file paths, or unsupported formats. Common Causes This error typically happens due to: Cross-Origin Resource Sharing (CORS) restrictions Incorrect file paths or missing media files Unsupported media formats Server configuration issues Method 1: Setting crossOrigin for CORS When loading media from different domains, set the crossOrigin attribute to handle CORS restrictions: const ...
Read MoreExample of createSignalingChannel() in HTML5
WebRTC enables peer-to-peer communication between browsers, requiring signaling for network information, session control, and media exchange. Developers can implement signaling using various protocols like SIP, XMPP, or custom two-way communication channels. What is createSignalingChannel()? The createSignalingChannel() function establishes a communication channel between peers to exchange connection information before direct peer-to-peer communication begins. This is essential for WebRTC setup. Complete Example var signalingChannel = createSignalingChannel(); var pc; var configuration = { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] }; // run start(true) to initiate a call function start(isCaller) { ...
Read MoreIndexed collections in JavaScript
JavaScript provides indexed collections that allow you to store and access elements using numeric indices. Arrays are the most common indexed collection, where each element has a position (index) starting from 0. What are Indexed Collections? Indexed collections are data structures where elements are accessed using numerical indices. In JavaScript, arrays are the primary indexed collection type, allowing you to store multiple values and retrieve them using their position. Basic Array Creation and Access Indexed Collections ...
Read More