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
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?
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 MoreCheck for Power of two in JavaScript
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 MoreHow to set the space between characters in a text with JavaScript?
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 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 MoreHow to set listStyleImage, listStylePosition, and listStyleType in one declaration with JavaScript?
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 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 MoreHow to set the distance between lines in a text with JavaScript?
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 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 MoreWhat is JavaScript garbage collection?
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 MoreReturn the maximum number in each array using map JavaScript
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