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 73 of 589
How do I clear the usage of setInterval()?
The setInterval() method executes a function repeatedly at specified intervals. Unlike setTimeout() which runs once, setInterval() continues indefinitely until cleared. Syntax let intervalId = setInterval(function, delay); Example: Basic setInterval Usage let count = 0; let intervalId = setInterval(function() { count++; console.log("Hello " + count); // Stop after 3 executions if (count === 3) { clearInterval(intervalId); ...
Read MoreHow to set the decoration of a text with JavaScript?
Use the textDecoration property in JavaScript to decorate text. This property allows you to add underlines, overlines, strikethrough effects, and more to any text element. Syntax element.style.textDecoration = "value"; Common textDecoration Values Value Effect underline Adds line below text overline Adds line above text line-through Strikes through text none Removes decoration Example: Basic Text Decoration Text Decoration Example This is demo text. ...
Read MoreHow to workaround Objects vs arrays in JavaScript for key/value pairs?
When you need key-value pairs in JavaScript, objects are the preferred choice over arrays. Objects provide direct key-based lookup, making data retrieval more efficient than searching through array indices. Why Objects Over Arrays for Key-Value Pairs Arrays use numeric indices and are optimized for ordered data, while objects are designed for key-value associations. Using objects allows you to access values directly by their keys. Basic Object Syntax Store key-value pairs using object literal syntax: var players = { 600: 'Sachin', 300: 'Brad' }; console.log(players[600]); // ...
Read MoreHow to set the top padding of an element with JavaScript?
Use the paddingTop property in JavaScript to set the top padding of an element. This property allows you to dynamically modify the spacing between an element's content and its top border. Syntax element.style.paddingTop = "value"; Where value can be specified in pixels (px), percentages (%), or other CSS units. Example #box { border: 2px solid #FF0000; ...
Read MoreWhy is [1,2] + [3,4] = "1,23,4" in JavaScript?
The JavaScript's + operator is used to add two numbers or join two strings. However, when used with arrays, it doesn't concatenate them as you might expect. Instead, it converts both arrays to strings and then concatenates those strings. What Happens with [1, 2] + [3, 4] When JavaScript encounters the + operator between two arrays, it follows these steps: Convert each array to a string using the toString() method Concatenate the resulting strings // Step 1: Arrays are converted to strings console.log([1, 2].toString()); // "1, 2" console.log([3, 4].toString()); // "3, ...
Read MoreHow to set the style of the line in a text decoration with JavaScript?
In this tutorial, we shall learn to set the style of the line in a text decoration with JavaScript. To set the style of the line in JavaScript, use the textDecorationStyle property. You can set underline, double, or overline, etc. for the line style. Using the Style textDecorationStyle Property We can set or return the line style in a text decoration with this property. The major browsers support this property. Firefox adds support with an alternate property named MozTextDecorationStyle. Syntax object.style.textDecorationStyle = "solid" | "double" | "dotted" | "dashed" | "wavy" | "initial" | "inherit"; ...
Read MoreWhat is to be done when text overflows the containing element with JavaScript?
When text overflows its container, JavaScript can help control the display behavior using the textOverflow property. This property works with CSS to handle overflow gracefully by adding ellipses or clipping the text. CSS Requirements For textOverflow to work properly, the element must have these CSS properties: overflow: hidden - Hide overflowing content white-space: nowrap - Prevent text wrapping Fixed width - Constrain the container size JavaScript textOverflow Values The textOverflow property accepts these values: "clip" - Cut off text at container edge "ellipsis" - Show "..." where text is cut "visible" ...
Read MoreIs it correct to use JavaScript Array.sort() method for shuffling?
No, it is not correct to use JavaScript's Array.sort() method for shuffling arrays. While it may appear to work, it produces biased results and is not a proper shuffling algorithm. Why Array.sort() Fails for Shuffling Using Array.sort(() => Math.random() - 0.5) seems like a clever shortcut, but it doesn't produce truly random shuffles. The sort algorithm expects consistent comparison results, but random comparisons violate this assumption. // WRONG: Biased shuffling with sort() let arr = [1, 2, 3, 4, 5]; let biasedShuffle = arr.sort(() => Math.random() - 0.5); console.log("Biased result:", biasedShuffle); Biased result: ...
Read MoreHow to set the capitalization of a text with JavaScript?
To set the capitalization, use the textTransform property in JavaScript. Set it to capitalize, if you want the first letter of every word to be a capital letter. Syntax element.style.textTransform = "value"; The textTransform property accepts these values: capitalize - Capitalizes the first letter of each word uppercase - Converts all text to uppercase lowercase - Converts all text to lowercase none - Removes any text transformation Example: Capitalizing Text You can try to run the following code to capitalize text with JavaScript: ...
Read MoreIs their a negative lookbehind equivalent in JavaScript?
JavaScript supports negative lookbehind assertions in modern environments (ES2018+), but older browsers require workarounds using character classes and capturing groups. Modern Negative Lookbehind (ES2018+) ES2018 introduced native negative lookbehind syntax (?: let text = 'He said "hello" and she said "goodbye"'; let result = text.replace(/(? breaks down as: (^|[^\]) — Captures either start of string OR any character except backslash " — Matches the quote to replace $1' — Replaces with the captured character plus single quote Browser Compatibility Comparison Method Browser Support Performance Native (? Chrome ...
Read More