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 116 of 589
Lambdas with Arrow Functions in JavaScriptLambdas with Arrow Functions in JavaScript
Lambda functions in JavaScript are small anonymous functions that can take parameters and return values. Arrow functions provide a concise way to write lambda functions and are commonly used for functional programming operations like map(), filter(), and reduce(). Syntax // Single parameter (parentheses optional) const lambda1 = x => x * 2; // Multiple parameters const lambda2 = (x, y) => x + y; // No parameters const lambda3 = () => "Hello World"; // Multiple statements (requires curly braces and return) const lambda4 = x => { const result ...
Read MoreHow to read data from JSON array using JavaScript?
JSON arrays are a common data structure in JavaScript applications. This article demonstrates various methods to read and access data from JSON arrays. What is a JSON Array? A JSON array is a collection of JSON objects enclosed in square brackets. Each object can contain multiple key-value pairs. [ {"name": "Rohan", "age": 22}, {"name": "Shawn", "age": 12}, {"name": "Michael", "age": 21} ] Method 1: Using JSON.parse() and forEach() The most common approach is parsing the JSON string and iterating through the array: ...
Read MoreEffective Function Signatures with Default and Rest Parameters in JavaScript
JavaScript function signatures with default and rest parameters allow you to create flexible functions that handle variable arguments and provide fallback values when parameters are missing. Default Parameters Default parameters provide fallback values when arguments are undefined or not passed: Default Parameters function greet(name = "Guest", greeting = "Hello") { return `${greeting}, ...
Read MoreHow to import an Object with Sub Objects and Arrays in JavaScript?
Importing objects with nested sub-objects and arrays in JavaScript using ES6 modules allows for modular code organization. This technique is essential for sharing complex data structures between different JavaScript files. What are ES6 Modules? ES6 modules use export and import statements to share code between files. The export default syntax allows exporting a single object, function, or value as the default export from a module. Example: Importing Complex Objects Let's create a complete example showing how to import an object containing nested objects and arrays. sample.js (Module file) export default { ...
Read MoreFormatted Strings Using Template Strings in JavaScript
Template strings (template literals) in JavaScript allow you to create formatted strings with embedded expressions using backticks (`). They provide a cleaner alternative to string concatenation and offer multiline support. Syntax `string text ${expression} string text` Basic Example Template Strings Example Template String Formatting Show Formatted String ...
Read MoreCheck whether a series of operations yields a given number with JavaScript Recursion
By starting from the number 1 and repeatedly either adding 5 or multiplying by 3, an infinite amount of new numbers can be produced. We are required to write a function that, given a number, tries to find a sequence of such additions and multiplications that produce that number. And returns a boolean based on the fact whether or not there exists any such sequence. For example, the number 13 could be reached by first multiplying by 3 and then adding 5 twice (1 × 3 = 3, 3 + 5 = 8, 8 + 5 = 13), so ...
Read MoreWhen you should not use JavaScript Arrow Functions?
Arrow functions should not be used in certain scenarios because they don't have their own this binding. Instead, they inherit this from the enclosing lexical scope, which can lead to unexpected behavior. When NOT to Use Arrow Functions Object Methods Arrow functions should not be used as object methods because they don't bind this to the object. Instead, this refers to the global scope (window in browsers). Arrow Functions - Object Methods Arrow Function vs Regular Function in Objects ...
Read MoreUsing '{ }' in JavaScript imports?
In JavaScript ES6 modules, curly braces { } are used for named imports, allowing you to import specific exported functions, variables, or classes from a module. Syntax // Named imports import { functionName, variableName } from './module.js'; // Named imports with alias import { originalName as newName } from './module.js'; // Mixed import import defaultExport, { namedExport } from './module.js'; Example JavaScript Named Imports ...
Read MoreChanging an array in place using splice() JavaScript
The splice() method allows you to modify arrays in place by removing, adding, or replacing elements. This article demonstrates how to use splice() to remove duplicate elements that exceed a specified count limit. The Problem We need to write a function that takes an array and a number n, then removes elements that appear more than n times while preserving the order of remaining elements. Solution Using splice() We'll track element counts using a hashmap and use splice() to remove excess occurrences during iteration: const arr = [7, 26, 21, 41, 43, 2, 26, ...
Read MoreImplement Private properties using closures in JavaScript
In JavaScript, closures provide a powerful way to create private properties that cannot be accessed directly from outside a function. This technique encapsulates data and prevents external code from modifying internal variables. What are Private Properties? Private properties are variables that can only be accessed and modified through specific methods, not directly from outside the containing scope. Closures enable this by keeping variables alive in memory even after the outer function has finished executing. How Closures Create Privacy When an inner function references variables from its outer function, it forms a closure. The outer function's variables ...
Read More