Front End Technology Articles

Page 381 of 652

Finding letter distance in strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 779 Views

We are required to write a JavaScript function that takes in a string as first argument and two single element strings. The function should return the distance between those single letter strings in the string taken as first argument. For example − If the three strings are − const str = 'Disaster management'; const a = 'i', b = 't'; Then the output should be 4 because the distance between 'i' and 't' is 4 Understanding Letter Distance Letter distance is the absolute difference between the index positions of two characters in ...

Read More

What is the importance of "enumerable" attribute in defining a property in JavaScript object?

vineeth.mariserla
vineeth.mariserla
Updated on 15-Mar-2026 394 Views

The enumerable attribute controls whether a property appears in object enumeration methods like for...in loops, Object.keys(), and JSON.stringify(). When using Object.defineProperty(), properties are non-enumerable by default. Syntax Object.defineProperty(objectName, propertyName, { value: propertyValue, enumerable: true/false }) Default Behavior: Non-enumerable Property When enumerable is not specified, it defaults to false, making the property invisible to enumeration methods: var object = {one: 1}; Object.defineProperty( object, ...

Read More

Function that parses number embedded in strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 184 Views

JavaScript's built-in functions like parseInt() and parseFloat() only parse numbers from the beginning of a string, stopping when they encounter non-numeric characters. When you need to extract all digits from anywhere within a string, you need a custom solution. The Problem with Built-in Methods Standard parsing methods fail with embedded numbers: console.log(parseInt('454ffdg54hg53')); // 454 (stops at first non-digit) console.log(parseFloat('12.34abc56.78')); // 12.34 (stops at 'a') 454 12.34 Method 1: Loop Through Characters This approach iterates through each character, extracting only digits: const numStr = '454ffdg54hg53'; ...

Read More

How to clone an array using spread operator in JavaScript?

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 3K+ Views

In this article, we are going to discuss how to use the spread operator to clone an array in JavaScript. Cloning is the process of copying one array into another array. Previously, the slice() method was used to clone an array, however, ES6 now provides the spread operator (...) to simply clone an array. What is Array Cloning? An Array is a data structure in JavaScript that can hold multiple values at once. Cloning involves copying an array's elements to create a new independent array. ...

Read More

What is the importance of _isEqual() method in JavaScript?

vineeth.mariserla
vineeth.mariserla
Updated on 15-Mar-2026 1K+ Views

The _.isEqual() method from Underscore.js and Lodash libraries provides deep equality comparison for JavaScript objects. Unlike native JavaScript comparison operators, it performs value-based comparison rather than reference-based comparison. Why _.isEqual() is Important JavaScript's native equality operators (== and ===) only check if two objects are the same reference in memory, not if they have the same content. The _.isEqual() method solves this by performing deep comparison of object properties, regardless of property order. Syntax _.isEqual(object1, object2); It accepts two values as parameters and returns true if they are equivalent, false otherwise. Example: ...

Read More

Matching strings for similar characters - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 674 Views

We are required to write a JavaScript function that accepts two strings and a number n. The function matches the two strings i.e., it checks if the two strings contains the same characters. The function returns true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, else the function should return false. Example Following is the code − const str = 'some random text'; const str2 = 'some r@ndom text'; const deviationMatching = (first, second, num) => { ...

Read More

How to convert a string to a floating point number in JavaScript?

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 2K+ Views

In JavaScript, converting a string to a floating point number is a common task that can be accomplished through several methods. This article explores three effective approaches to achieve this conversion. Using the parseFloat() Method The parseFloat() function is the most direct way to convert a string to a floating point number. It parses a string and returns the first number found, including decimal values. Syntax parseFloat(string) Note: If the first character cannot be converted to a number, parseFloat() returns NaN. Example: Basic parseFloat() Usage ...

Read More

Is it possible to change the HTML content in JavaScript?

vineeth.mariserla
vineeth.mariserla
Updated on 15-Mar-2026 2K+ Views

Yes, it is possible to change HTML content using JavaScript. JavaScript provides DOM (Document Object Model) methods that can access and modify HTML elements. These methods, such as document.getElementById(), document.getElementsByTagName(), and others, allow you to dynamically update content in HTML tags like , , , etc. Common Methods to Change HTML Content The most frequently used properties for changing HTML content are: innerHTML - Changes the HTML content inside an element textContent - Changes only the text content (no HTML tags) innerText - Similar to textContent but respects styling Example 1: Changing Content with ...

Read More

What is the use of _.size() method in JavaScript?

vineeth.mariserla
vineeth.mariserla
Updated on 15-Mar-2026 696 Views

The _.size() method is part of the Underscore.js library and returns the number of elements in arrays, objects, and strings. It provides a consistent way to get the size of various data types. Syntax _.size(collection) Parameters collection: The array, object, or string to count elements from. Return Value Returns a number representing the count of elements in the collection. Example 1: Array Size Find the size of a numeric array using _.size(): var arr = [100, 62, 73, 534, 565]; document.write("Array size: ...

Read More

Splitting last n digits of each value in the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 163 Views

We have an array of numbers and need to extract the last n digits from each value. If a number has fewer than n digits, it remains unchanged. const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799]; console.log("Original array:", arr); Original array: [ 56768, 5465, 5467, 3, 878, 878, 34435, 78799 ] We need a function that takes an array and a number n, then returns the last n digits of each element. If an element has fewer than n digits, it stays the same. Example with n = 2 ...

Read More
Showing 3801–3810 of 6,519 articles
« Prev 1 379 380 381 382 383 652 Next »
Advertisements