Object Oriented Programming Articles

Page 125 of 589

How to assign static methods to a class in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 792 Views

To assign static methods to a class in JavaScript, prefix the method with the static keyword. Static methods belong to the class itself rather than instances and can be called without creating an object. Syntax class ClassName { static methodName() { // method body } } // Call static method ClassName.methodName(); Basic Static Method Example class Calculator { static add(a, b) { return a ...

Read More

Reverse all the words of sentence JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 4K+ Views

We are required to write a JavaScript function that takes in a string and returns a new string which has all the words reversed from the original string. For example − If the original string is − "Hello World how is it outside" Then the output should be − "olleH dlroW woh si ti edistuo" Now, let's write the code for this function − Example const str = 'Hello World how is it outside'; const reverseSentence = str => { const arr = ...

Read More

Setting property in an empty object using for loop in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 510 Views

In JavaScript, you can populate an empty object with properties using various loop methods. The most common approaches are the for...in loop and the for...of loop with Object.entries(). Using for...in Loop The for...in loop iterates over all enumerable properties of an object, making it ideal for copying properties from one object to another. Setting Properties with for...in Loop Setting Properties in Empty Object Populate ...

Read More

Single dimensional array vs multidimensional array in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 821 Views

JavaScript supports both single-dimensional and multidimensional arrays. A single-dimensional array stores elements in a linear sequence, while a multidimensional array contains arrays as elements, creating a matrix-like structure. Single-Dimensional Arrays A single-dimensional array is a simple list of elements accessed by a single index. Single Dimensional Array let singleArray = [10, 20, 30, 40, 50]; console.log("Single-dimensional array:", ...

Read More

How to draw a circle in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Drawing circles in JavaScript is accomplished using the HTML5 Canvas API and the arc() method. This method allows you to create perfect circles and arcs with precise control over position, size, and styling. Basic Circle Drawing Syntax context.arc(x, y, radius, startAngle, endAngle, counterclockwise); Parameters Parameter Description x, y Center coordinates of the circle radius Circle radius in pixels startAngle Starting angle (0 for full circle) endAngle Ending angle (2 * Math.PI for full circle) Interactive Circle Example ...

Read More

Check for Ugly number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In the decimal number system, ugly numbers are positive integers whose only prime factors are 2, 3, or 5. This means an ugly number can be expressed as 2i × 3j × 5k where i, j, and k are non-negative integers. For example, the integers 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 are all ugly numbers because they only contain the prime factors 2, 3, and 5. However, 7, 11, 13, 14 are not ugly numbers as they contain other prime factors. Algorithm To check if a number is ugly, we repeatedly divide it ...

Read More

How to create a unique ID for each object in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In JavaScript, creating unique IDs for objects is essential for tracking and identifying instances. The most reliable method is using Symbol(), which generates guaranteed unique identifiers. Using Symbol() for Unique IDs The Symbol() function creates a unique symbol every time it's called, even with the same description. This makes it perfect for generating unique object identifiers. Unique Object IDs body { ...

Read More

How to access an object through another object in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 535 Views

In JavaScript, you can access properties and methods of one object from another object by referencing them directly. This technique is useful for sharing data between objects or creating relationships. Basic Property Access The simplest way is to reference properties directly from another object: Object Access Example CLICK HERE // First object with properties and method let obj = { firstName: "Rohan", lastName: "Sharma", welcome() { ...

Read More

Leaders array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 421 Views

An element in an array is a leader if it is greater than all elements on its right side. The rightmost element is always a leader since there are no elements to its right. For example, in the array [23, 55, 2, 56, 3, 6, 7, 1]: 56 is a leader (greater than 3, 6, 7, 1) 7 is a leader (greater than 1) 1 is a leader (rightmost element) Input: [23, 55, 2, 56, 3, 6, 7, 1] Output: [56, 7, 1] Using reduceRight() Method The most efficient approach is ...

Read More

How to create a multidimensional JavaScript object?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 4K+ Views

A multidimensional JavaScript object is an object that contains other objects as properties, creating nested structures. This allows you to organize complex data hierarchically. Basic Syntax let multidimensionalObject = { property1: "value1", property2: { nestedProperty1: "nestedValue1", nestedProperty2: { deeplyNestedProperty: "deeplyNestedValue" } } }; Example: Creating a Student ...

Read More
Showing 1241–1250 of 5,881 articles
« Prev 1 123 124 125 126 127 589 Next »
Advertisements