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 118 of 589
The new operator in JavaScript
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 MoreCheck if three consecutive elements in an array is identical in JavaScript
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 MoreDoes JavaScript support block scope?
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 MoreFormatting dynamic json array JavaScript
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 MoreJavaScript code to print last element of an array
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 MoreFormatting JavaScript Object to new Array
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 MoreShort Circuit Assignment in JavaScript
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 MoreCan we assign new property to an object using deconstruction in JavaScript?
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 MoreFinding matches in two elements JavaScript
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 MoreObject.keys().map() VS Array.map() in JavaScript
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