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 127 of 589
Separate odd and even in JavaScript
In JavaScript, separating odd and even numbers means rearranging an array so that all even numbers appear before all odd numbers. This is commonly achieved using custom sorting functions or array methods like filter(). Using Custom Sort Function The most efficient approach uses Array.sort() with a custom comparator that prioritizes even numbers: const arr = [2, 6, 3, 7, 8, 3, 5, 4, 3, 6, 87, 23, 2, 23, 67, 4]; const isEven = num => num % 2 === 0; const sorter = (a, b) => { if(isEven(a) && !isEven(b)){ ...
Read MoreFunction borrowing in JavaScript.
Function borrowing in JavaScript allows objects to use methods from other objects without inheriting from them. This is achieved using call(), apply(), and bind() methods. Understanding Function Borrowing When an object doesn't have a particular method but needs to use it, it can "borrow" that method from another object. The borrowed method executes in the context of the borrowing object using the this keyword. Using call() Method The call() method invokes a function with a specific this context and individual arguments. Function Borrowing with call() ...
Read MoreNesting template strings in JavaScript
Nesting template strings in JavaScript allows you to embed one template literal inside another. This is useful when building complex strings with dynamic content or when passing template literals as function arguments. What are Nested Template Strings? Nested template strings occur when you place one template literal (using backticks) inside another. The inner template string is evaluated first, then the outer one. Basic Syntax `Outer template ${`Inner template ${variable}`} continues here` Simple Example Nested ...
Read MoreJavaScript Return an array that contains all the strings appearing in all the subarrays
We have an array of arrays like this − const arr = [ ['foo', 'bar', 'hey', 'oi'], ['foo', 'bar', 'hey'], ['foo', 'bar', 'anything'], ['bar', 'anything'] ] We are required to write a JavaScript function that takes in such array and returns an array that contains all the strings which appears in all the subarrays. Let's write the code for this function Example const arr = [ ['foo', 'bar', 'hey', 'oi'], ['foo', 'bar', 'hey'], ...
Read MoreAwaiting on dynamic imports in JavaScript.
Dynamic imports in JavaScript allow you to load modules asynchronously at runtime using the import() function. This is particularly useful for code splitting and loading modules only when needed. Syntax const module = await import('./modulePath.js'); // or import('./modulePath.js').then(module => { // use module }); How Dynamic Imports Work The import() function returns a Promise that resolves to the module object. You can use await to handle it asynchronously: Dynamic Imports Example ...
Read MoreES6 Property Shorthands in JavaScript
ES6 introduced property shorthand syntax that simplifies object creation when the property name matches the variable name. Instead of writing name: name, you can simply write name. Syntax // ES5 way let obj = { property: property, anotherProperty: anotherProperty }; // ES6 shorthand let obj = { property, anotherProperty }; Basic Example ES6 Property Shorthands ...
Read MoreHow to split last n digits of each value in the array with JavaScript?
We have an array of mixed values like numbers and strings, and we want to extract the last n digits from each element that has enough characters. const arr = ["", 20191219, 20191220, 20191221, 20191222, 20191223, 20191224, 20191225]; We need to write a JavaScript function that takes this array and a number n. If an element contains more than or equal to n characters, the function should return only the last n characters. Otherwise, the element should remain unchanged. Solution Here's how we can implement this function using the map() method and string manipulation: ...
Read MoreFirst class function in JavaScript
JavaScript treats functions as first-class citizens, meaning they can be stored in variables, passed as arguments, and returned from other functions. This powerful feature enables functional programming patterns and higher-order functions. What are First-Class Functions? In JavaScript, functions are first-class objects, which means they have all the capabilities of other objects. They can be: Stored in variables and data structures Passed as arguments to other functions Returned as values from functions Assigned properties and methods Storing Functions in Variables First-Class Functions // Store function ...
Read MoreAsynchronous Functions and the Node Event Loop in Javascript
Asynchronous functions allow programs to continue executing without waiting for time-consuming operations to complete. This non-blocking behavior is fundamental to JavaScript and Node.js, enabling better user experience and efficient resource utilization. When executing expensive operations like network requests or file I/O, asynchronous functions prevent the entire program from freezing. Instead of blocking execution, these operations run in the background while other code continues to execute. Understanding Asynchronous Execution In synchronous programming, each operation must complete before the next one begins. Asynchronous programming allows multiple operations to run concurrently, with callbacks or promises handling completion. console.log('One'); ...
Read MoreIs it possible to have JavaScript split() start at index 1?
The built-in String.prototype.split() method doesn't have a parameter to start splitting from a specific index. However, we can combine split() with array methods like slice() to achieve this functionality. Problem with Standard split() The standard split() method always starts from index 0 and splits the entire string: const text = "The quick brown fox jumped over the wall"; console.log(text.split(" ")); [ 'The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'wall' ] Method 1: Using split() with slice() The simplest ...
Read More