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
Length of a JavaScript associative array?
In JavaScript, arrays don't truly support associative arrays (key-value pairs with string keys). When you assign string keys to an array, they become object properties, not array elements, so the length property returns 0. To get the count of properties, use Object.keys(). The Problem with Array.length When you add string keys to an array, they don't count as array elements: var details = new Array(); details["Name"] = "John"; details["Age"] = 21; details["CountryName"] = "US"; details["SubjectName"] = "JavaScript"; console.log("Array length:", details.length); // 0, not 4! console.log("Type:", typeof details); Array length: 0 Type: ...
Read MoreHow can we make an Array of Objects from n properties of n arrays in JavaScript?
When working with multiple arrays in JavaScript, you often need to combine them into an array of objects. This is useful for creating structured data from separate arrays of related information. Suppose we have two arrays of literals like these: const options = ['A', 'B', 'C', 'D']; const values = [true, false, false, false]; We need to create a JavaScript function that combines these arrays into a new array of objects like this: [ {opt: 'A', val: true}, {opt: 'B', val: false}, {opt: 'C', val: false}, ...
Read MoreHow to convert a MySQL date to JavaScript date?
In this tutorial, we will learn to convert the MySQL date to JavaScript date. The MySQL date is not different from the regular date, but its format or syntax is different, which we need to convert into the format of a normal date. The general syntax of the MySQL date is YYYY-MM-DD HH:mm:ss. So, we need to convert the given MySQL date string syntax to normal date syntax. We will have two approaches to converting the MySQL date to the JavaScript date. Using the replace() Method Using the split() Method ...
Read MoreHow to stream large .mp4 files in HTML5?
Streaming large MP4 files in HTML5 requires proper video encoding and server configuration to enable progressive download while the video plays. Video Encoding Requirements For HTML5 streaming, MP4 files need special encoding where metadata is moved to the beginning of the file. This allows browsers to start playback before the entire file downloads. Using mp4FastStart The mp4FastStart tool relocates metadata to enable streaming: // Command line usage mp4faststart input.mp4 output.mp4 Using HandBrake HandBrake video encoder includes a "Web Optimized" option that automatically prepares MP4 files for streaming by moving the metadata ...
Read MoreShortest path algorithms in Javascript
In graph theory, the shortest path problem is finding a path between two vertices in a graph such that the sum of edge weights is minimized. To implement shortest path algorithms, we need to modify our graph structure to support weighted edges. Setting Up Weighted Graphs First, let's modify our graph methods to handle weights: class WeightedGraph { constructor() { this.nodes = []; this.edges = {}; } ...
Read MoreArrays vs Set in JavaScript.
In this article, we will learn about the difference between an array and a set in JavaScript. Arrays are used to store ordered collections of elements, whereas Sets store only unique values in an unordered collection. What is an Array? An Array is an ordered, indexed collection of values in JavaScript. It allows duplicate values and provides various built-in methods to manipulate elements. Syntax let numbers = [1, 2, 3, 4, 5]; What is a Set? A Set is an unordered collection of unique values in JavaScript. Unlike arrays, a Set automatically ...
Read MorePossible to split a string with separator after every word in JavaScript
To split a string with separator after every word, you can use the split() method combined with filter() to remove empty elements that may result from consecutive separators. Syntax let result = string.split('separator').filter(value => value); Basic Example Let's start with a string that has separators between words: let sentence = "-My-Name-is-John-Smith-I-live-in-US"; console.log("Original string:", sentence); let result = sentence.split('-').filter(value => value); console.log("After split():"); console.log(result); Original string: -My-Name-is-John-Smith-I-live-in-US After split(): [ 'My', 'Name', 'is', 'John', 'Smith', 'I', 'live', ...
Read MoreFinding letter distance in strings - JavaScript
We are required to write a JavaScript function that takes in a string as first argument and two single element strings. The function should return the distance between those single letter strings in the string taken as first argument. For example − If the three strings are − const str = 'Disaster management'; const a = 'i', b = 't'; Then the output should be 4 because the distance between 'i' and 't' is 4 Understanding Letter Distance Letter distance is the absolute difference between the index positions of two characters in ...
Read MoreHow to reduce the number of errors in scripts?
Reducing errors in JavaScript scripts is crucial for maintainable code. Following established best practices can significantly improve code quality and reduce debugging time. Essential Practices for Error-Free Scripts Use Meaningful Comments Comments explain the purpose and logic behind your code, making it easier to understand and maintain. // Calculate total price including tax function calculateTotal(price, taxRate) { // Apply tax rate as percentage const tax = price * (taxRate / 100); return price + tax; } console.log(calculateTotal(100, 8.5)); // $100 with 8.5% ...
Read MorePlay infinitely looping video on-load in HTML5
HTML5 provides built-in support for playing videos that loop infinitely using the loop attribute. This is useful for background videos, animations, or promotional content that should continuously play. Basic Syntax The element supports three main video formats: MP4, WebM, and Ogg. To create an infinitely looping video, combine the autoplay and loop attributes: Your browser does not support the video element. Key Attributes autoplay: Starts the video automatically when the page loads loop: A boolean attribute that restarts ...
Read More