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
Object Oriented Programming Articles
Page 123 of 589
Keyed collections in JavaScript
Keyed collections are data structures in JavaScript where elements are organized by keys rather than indices. The two main keyed collections are Map and Set objects, which maintain insertion order and provide efficient key-based operations. Map Collection A Map object holds key-value pairs and remembers the original insertion order of the keys. Unlike objects, Map keys can be of any type. Map Collection Example Map Collection Demo ...
Read MoreLoading JavaScript modules dynamically
Dynamic module loading allows you to import JavaScript modules at runtime rather than at compile time. This enables lazy loading, conditional imports, and better performance optimization. Static vs Dynamic Import Static imports happen at the top of files and load immediately. Dynamic imports use the import() function to load modules on demand. Dynamic Module Loading body { ...
Read MoreRenaming imports and exports in JavaScript
JavaScript ES6 modules allow you to rename imports and exports using the as keyword. This is useful for avoiding naming conflicts, improving code readability, or providing more descriptive names for functions and variables. Note − You need to run a localhost server to run this example due to ES6 module requirements. Syntax For renaming exports: export { originalName as newName }; For renaming imports: import { originalName as newName } from './module.js'; Example Let's create a complete example with three files demonstrating both import and export renaming: ...
Read MoreFind the longest sub array of consecutive numbers with a while loop in JavaScript
We are required to write a function with a while-statement that determines the length of the largest consecutive subarray in an array of positive integers. For instance, if we have an array like [6, 7, 8, 6, 12, 1, 2, 3, 4], the longest consecutive sequence is [1, 2, 3, 4] with length 4. Problem Understanding Let's look at some examples to understand the problem better: Input: [6, 7, 8, 6, 12, 1, 2, 3, 4] Consecutive sequence: [1, 2, 3, 4] Output: 4 Input: [5, 6, 1, 8, 9, 7] Consecutive ...
Read Moreloose equality in JavaScript
The loose equality operator == compares two values after performing type coercion, converting them to a common type before comparison. This differs from strict equality (===) which compares both value and type. Syntax operand1 == operand2 How Loose Equality Works When using ==, JavaScript follows specific conversion rules: If types are the same, compare values directly If one is a number and the other a string, convert string to number If one is boolean, convert to number first null and undefined are equal to each other Example ...
Read MoreStrict equality vs Loose equality in JavaScript.
In JavaScript, there are two ways to compare values for equality: loose equality (==) and strict equality (===). Understanding the difference is crucial for writing reliable code. Loose Equality (==) The loose equality operator == compares values after converting them to a common type (type coercion). This can lead to unexpected results. Loose Equality Examples Loose Equality Results ...
Read MoreAccessing an array returned by a function in JavaScript
In JavaScript, functions can return arrays that can be accessed and manipulated directly. This is useful for creating reusable code that generates data structures. Basic Syntax function functionName() { let array = [/* elements */]; return array; } // Access the returned array let result = functionName(); Example: Returning and Accessing Arrays Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...
Read MoreSimplifying nested array JavaScript
Let's say, we have an array of arrays that contains some elements like this − const arr = [3, 5, 7, 2, [4, NaN, null, 4, 8, [3, undefined, 24, null], null, 5, 1], NaN, 45, 2, 1]; Our job is to write a recursive function that takes in this nested array and replaces all the falsy values inside the array (NaN, undefined and null) with 0. Understanding Falsy Values In JavaScript, falsy values include: null undefined NaN ...
Read MoreObject initializer in JavaScript
An object initializer is an expression that allows us to initialize a newly created object. It is a comma-separated list of zero or more pairs of property names and associated values of an object, enclosed in a pair of curly braces {}. Basic Syntax const objectName = { property1: value1, property2: value2, // ... more properties }; Simple Object Creation Here's a basic example of creating an object using object initializer syntax: ...
Read MoreThe new.target in JavaScript
The new.target is a metaproperty that allows us to determine at runtime whether a function or constructor was called using the new keyword or not. It returns undefined when called as a regular function and references the constructor when called with new. Basic Syntax function MyConstructor() { if (new.target) { // Called with 'new' } else { // Called as regular function } } Example: Detecting Constructor ...
Read More