Javascript Articles

Page 342 of 534

How to deal with Internet Explorer and addEventListener problem "Object doesn't support this property or method" in JavaScript?

George John
George John
Updated on 15-Mar-2026 2K+ Views

Internet Explorer versions 8 and below don't support the standard addEventListener() method, causing the "Object doesn't support this property or method" error. This article shows how to handle cross-browser event handling. The Problem Modern browsers use addEventListener() for event handling, but older Internet Explorer versions use attachEvent() instead. Using addEventListener() directly will fail in IE8 and below. Solution 1: Using IE=edge Meta Tag Force Internet Explorer to use its latest rendering engine by adding this meta tag to your HTML head: ...

Read More

Check for Power of two in JavaScript

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

We need to write a function, isPowerOfTwo(), that takes a positive number and returns a boolean based on whether the number is a power of 2. For example: isPowerOfTwo(3) // false (3 is not a power of 2) isPowerOfTwo(32) // true (32 = 2^5) isPowerOfTwo(2048) // true (2048 = 2^11) isPowerOfTwo(256) // true (256 = 2^8) isPowerOfTwo(22) // false (22 is not a power of 2) Method 1: Using Recursive Division This approach recursively divides the number by 2 until it reaches 1 (power of 2) ...

Read More

How to set the space between characters in a text with JavaScript?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 509 Views

To set the space between characters in text, use the JavaScript letterSpacing property. This property controls the horizontal spacing between individual characters in a text element. Syntax element.style.letterSpacing = "value"; The value can be specified in pixels (px), em units, or other CSS length units. Negative values decrease spacing, while positive values increase it. Example: Setting Letter Spacing Here's a complete example that demonstrates how to set character spacing with JavaScript: Letter Spacing Example ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 894 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

How to set listStyleImage, listStylePosition, and listStyleType in one declaration with JavaScript?

Sharon Christine
Sharon Christine
Updated on 15-Mar-2026 155 Views

To set the list properties in a single declaration, use the listStyle property in JavaScript. This shorthand property allows you to set listStyleType, listStylePosition, and listStyleImage all at once. Syntax element.style.listStyle = "type position image"; Parameters The listStyle property accepts up to three values: listStyleType: disc, circle, square, decimal, none, etc. listStylePosition: inside, outside listStyleImage: url() or none Example: Setting Type and Position Apple ...

Read More

Formatting dynamic json array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 881 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

How to set the distance between lines in a text with JavaScript?

Paul Richard
Paul Richard
Updated on 15-Mar-2026 530 Views

To set the distance between lines in text, use the lineHeight CSS property via JavaScript. This property controls the vertical spacing between lines of text within an element. Syntax element.style.lineHeight = value; The lineHeight value can be: Number: Multiplier of font size (e.g., "2" = 2x font size) Pixels: Fixed height (e.g., "30px") Percentage: Relative to font size (e.g., "150%") Example: Setting Line Height with Button Click Line Height Demo ...

Read More

Finding matches in two elements JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 255 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

What is JavaScript garbage collection?

Vrundesha Joshi
Vrundesha Joshi
Updated on 15-Mar-2026 415 Views

JavaScript automatically manages memory allocation and deallocation through a process called garbage collection. When you declare variables or create objects, JavaScript allocates memory for them. The garbage collector then identifies and frees memory that is no longer needed by your application. How Garbage Collection Works JavaScript's garbage collector runs automatically in the background, scanning memory to find objects that are no longer reachable or referenced by your code. When these "orphaned" objects are found, their memory is freed up for reuse. Mark-and-Sweep Algorithm The most common garbage collection algorithm in modern JavaScript engines is the mark-and-sweep ...

Read More

Return the maximum number in each array using map JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 790 Views

We have an array of arrays of Numbers like this one: const arr = [ [12, 56, 34, 88], [65, 66, 32, 98], [43, 87, 65, 43], [32, 98, 76, 83], [65, 89, 32, 54], ]; We are required to write a function that maps over this array of arrays and returns an array that contains the maximum (greatest) element from each subarray. Using Math.max() with Spread Operator The most straightforward approach is to use Math.max() with the spread operator to find the maximum ...

Read More
Showing 3411–3420 of 5,340 articles
« Prev 1 340 341 342 343 344 534 Next »
Advertisements