Javascript Articles

Page 331 of 534

How to set the width of an element in JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 12K+ Views

In this tutorial, we will learn to set the width of an element in JavaScript. We can use the "width" property in JavaScript to set the width of an element. The web page should be responsive on every screen. Each element on a web page should have its area covered. We can set the size of elements by setting their width and height. Let us look at how to set the width of an element in JavaScript. Following is the property by which we can set the width of an element in JavaScript − ...

Read More

How to set the width of an element's border with JavaScript?

Sai Subramanyam
Sai Subramanyam
Updated on 15-Mar-2026 1K+ Views

To set the width of an element's border in JavaScript, use the borderWidth property. This property allows you to dynamically modify border thickness after the page loads. Syntax element.style.borderWidth = "value"; The value can be specified in pixels (px), keywords (thin, medium, thick), or other CSS units. Example: Changing Border Width #box { border: thick solid gray; ...

Read More

Convert JavaScript array iteration result into a single line text string

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 352 Views

Let's say we have a string and an array of keywords to search for: const textString = 'Convert javascript array iteration result into a single line text string. Happy searching!'; const keywords = ['integer', 'javascript', 'dry', 'Happy', 'exam']; We need to write a function that maps the array to a string containing only true and false values, depending on whether each array element is present in the string or not. Solution Using reduce() and join() The most efficient approach uses reduce() to build an array of boolean values, then join() to convert it into ...

Read More

How to set line breaking rules for non-CJK scripts in JavaScript?

Govinda Sai
Govinda Sai
Updated on 15-Mar-2026 345 Views

The word-break CSS property controls how words break when text overflows its container. For non-CJK (Chinese, Japanese, Korean) scripts like English, you can set different line breaking behaviors using JavaScript. Syntax element.style.wordBreak = "value"; word-break Property Values Value Description Use Case normal Default breaking rules Standard text flow break-all Break anywhere, even mid-word Prevent overflow in narrow containers keep-all Don't break CJK text Preserve CJK word integrity Example: Interactive Word Breaking ...

Read More

Append the current array with the squares of corresponding elements of the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 163 Views

We have an array of Numbers like this: const arr = [12, 19, 5, 7, 9, 11, 21, 4]; We have to write a function that takes in such an array and returns a new array with all the items of the original array appended by the squares of corresponding elements of the array. For this sample array, the output should be: [12, 19, 5, 7, 9, 11, 21, 4, 144, 361, 25, 49, 81, 121, 441, 16] Method 1: Using reduce() The reduce() method can accumulate the original array ...

Read More

How to set the spacing between words in a text in JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 5K+ Views

In this article, we will learn how to set the spacing between words in a text using JavaScript. Word spacing is critical on websites because it improves the legibility and readability of the text displayed on the page. You can generate an aesthetically pleasing and easy-to-read typeface using proper word spacing. In JavaScript, we use the wordSpacing property to set the spacing between the words in a text displayed on a website. Using the Style wordSpacing Property This JavaScript DOM property is used to get and set the spacing between the words in a text. You ...

Read More

Adding only odd or even numbers JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 983 Views

We are required to make a function that given an array of numbers and a string that can take any of the two values "odd" or "even", adds the numbers which match that condition. If no values match the condition, 0 should be returned. For example: conditionalSum([1, 2, 3, 4, 5], "even") => 6 conditionalSum([1, 2, 3, 4, 5], "odd") => 9 conditionalSum([13, 88, 12, 44, 99], "even") => 144 conditionalSum([], "odd") => 0 Using Array.reduce() Method We'll use the Array.prototype.reduce() method to iterate through the array and sum numbers based on the condition: ...

Read More

How to set the bottom position of a positioned element with JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 2K+ Views

In this tutorial, we will learn how to set the bottom position of a positioned element with JavaScript. The position property sets how the element should be positioned in the DOM, and the top, bottom, left, and right properties set the final location of the positioned elements. In JavaScript, we have different ways to set the bottom position of a positioned element, and in this tutorial, we will learn two of them − Using the style.bottom Property Using the style.setProperty Method Using the style.bottom Property In ...

Read More

How to return which part of a positioned element is visible in JavaScript?

Prabhdeep Singh
Prabhdeep Singh
Updated on 15-Mar-2026 280 Views

This tutorial teaches us how to return which part of a positioned element is visible in JavaScript. A positioned element is an HTML element with CSS positioning (absolute, relative, fixed, etc.). To make only a defined part visible, we use the CSS clip property to hide unwanted areas. The clip property defines a rectangular clipping region that determines which portion of an absolutely positioned element should be visible. Syntax Here's the basic syntax for clipping an element and retrieving the clip value: document.getElementById("elementId").style.clip = "rect(top right bottom left)"; let clipValue = document.getElementById("elementId").style.clip; The ...

Read More

Make array numbers negative JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 968 Views

In JavaScript, you can convert all positive numbers in an array to negative while keeping already negative numbers unchanged. This is useful for data transformations where you need to invert positive values. Let's say we have the following array: const arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6]; We need to write a function that converts all positive numbers to negative while leaving negative numbers unchanged (like 4 to -4, 6 to -6, but -12 stays -12). Using Array.reduce() const arr = [7, 2, 3, 4, 5, ...

Read More
Showing 3301–3310 of 5,340 articles
« Prev 1 329 330 331 332 333 534 Next »
Advertisements