Object Oriented Programming Articles

Page 118 of 589

The new operator in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 485 Views

The new operator in JavaScript creates instances of user-defined objects or built-in object types that have constructor functions. When used with a constructor function, it creates a new object, sets up the prototype chain, and returns the newly created object. Syntax new constructor([arguments]) How the new Operator Works When you use the new operator, JavaScript performs these steps: Creates a new empty object Sets the object's prototype to the constructor's prototype Calls the constructor function with this pointing to the new object ...

Read More

Check if three consecutive elements in an array is identical in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 900 Views

We are required to write a JavaScript function, say checkThree() that takes in an array and returns true if anywhere in the array there exists three consecutive elements that are identical (i.e., have the same value) otherwise it returns false. Therefore, let's write the code for this function − Example const arr = ["g", "z", "z", "v" ,"b", "b", "b"]; const checkThree = arr => { const prev = { element: null, count: 0 ...

Read More

Does JavaScript support block scope?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 283 Views

JavaScript supports block scope for variables declared with let and const, but not for variables declared with var. Understanding this difference is crucial for writing predictable JavaScript code. What is Block Scope? Block scope means a variable is only accessible within the curly braces {} where it was declared. This includes if statements, for loops, while loops, and any other code blocks. Variables with let and const (Block Scoped) Variables declared with let and const are confined to their block: Block Scope with let/const ...

Read More

Formatting dynamic json array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 889 Views

Let's say, we have an array of objects like this – const arr = [ {"name1": "firstString"}, {"name2": "secondString"}, {"name3": "thirdString"}, {"name4": "fourthString"}, {"name5": "fifthString"}, {"name6": "sixthString"}, ]; We are required to write a function that takes one such array of objects and returns an object with all the properties listed in that object. So, let's write the code for this function. It can be done through the Array reduce method – Using Array.reduce() Method ...

Read More

JavaScript code to print last element of an array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 279 Views

Getting the last element of an array is a common operation in JavaScript. There are several methods to accomplish this, with different approaches depending on whether you want to modify the original array or not. Using Array Length (Recommended) The most efficient method uses the array's length property to access the last index: Last Array Element Array: ["A", 1, 2, 3, "B", "D"] Get Last Element ...

Read More

Formatting JavaScript Object to new Array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 580 Views

In JavaScript, you can format objects into arrays using various methods like destructuring, Object.keys(), Object.values(), or custom formatting based on your needs. This is useful for data transformation and display purposes. Example: Formatting Object Properties to Array Format Object to Array body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...

Read More

Short Circuit Assignment in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 264 Views

Short circuit assignment in JavaScript uses logical operators (|| and &&) to assign values based on boolean evaluation. These operators stop evaluating as soon as the result is determined. How Short Circuit Assignment Works The || (OR) operator returns the first truthy value it encounters, while && (AND) returns the first falsy value or the last value if all are truthy. Example with OR Operator (||) Short Circuit Assignment Short Circuit Assignment ...

Read More

Can we assign new property to an object using deconstruction in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 183 Views

You can assign new properties to an object using destructuring in JavaScript. This technique allows you to extract values from one object and assign them as properties to another object. Basic Syntax // Destructuring assignment to object properties ({ property1: targetObj.newProp1, property2: targetObj.newProp2 } = sourceObj); Example Object Destructuring Assignment body { ...

Read More

Finding matches in two elements JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 261 Views

We are required to write a function that returns true if the string in the first element of the array contains all of the letters of the string in the second element of the array. For example: ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring their case. The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y". Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien". This ...

Read More

Object.keys().map() VS Array.map() in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 860 Views

In JavaScript, Object.keys().map() and Array.map() serve different purposes. Object.keys() extracts object keys as an array, then applies map(), while Array.map() directly transforms array elements. Understanding Object.keys() Object.keys() returns an array of an object's property names (keys). When chained with map(), it allows you to transform these keys. Object Keys Example let obj = { ...

Read More
Showing 1171–1180 of 5,881 articles
« Prev 1 116 117 118 119 120 589 Next »
Advertisements