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
Articles by vineeth.mariserla
Page 6 of 8
What is importance of startsWith() method in JavaScript?
To check whether a string starts with a particular character or substring, the indexOf() method was traditionally used. However, ES6 introduced the more intuitive and readable startsWith() method, which is specifically designed for this purpose and offers better performance and clarity. Traditional Approach: Using indexOf() The indexOf() method returns the first index where a substring is found. To check if a string starts with specific characters, we compare the result with 0: var text = 'Tutorialspoint'; document.write(text.indexOf('T') === 0); true ...
Read MoreWhat is the use of test() method in JavaScript?
The test() method is a regular expression method that searches a string for a pattern and returns true or false, depending on whether the pattern is found. It is case sensitive and provides a simple way to check if a string matches a regular expression pattern. Syntax regexPattern.test(string) Parameters string - The string to be tested against the regular expression pattern Return Value Returns true if the pattern is found in the string, false otherwise. Example 1: Pattern Found (Case Match) In this example, ...
Read MoreHow to freeze an Object in JavaScript?
In JavaScript, Object.freeze() makes an object immutable by preventing modifications to its properties. Once frozen, you cannot add, delete, or modify properties of the object. Syntax Object.freeze(obj) Parameters obj: The object to freeze. Returns the same object that was passed to the function. How Object.freeze() Works When an object is frozen: New properties cannot be added Existing properties cannot be removed Existing property values cannot be changed The object's prototype cannot be changed Example: Basic Object Freezing // Create an object with properties ...
Read MoreHow to remove the last digit of a number and execute the remaining digits in JavaScript?
In JavaScript, you can remove the last digit from a number using different approaches. The most common methods involve string manipulation or mathematical operations like bitwise operations. When working with numbers, you typically need to convert the number to a string to use string methods, or use mathematical operations to avoid type conversion altogether. Method 1: Using String substring() The substring() method extracts characters from a string between two specified indices. Syntax str.substring(startIndex, endIndex) To remove the last digit, use str.substring(0, str.length - 1) where the first parameter is the starting index ...
Read MoreWhat is the use of _.size() method in JavaScript?
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 MoreHow to count a number of words in given string in JavaScript?
Counting words in a JavaScript string requires handling various whitespace scenarios like multiple spaces, leading/trailing spaces, and newlines. Using regular expressions provides an effective solution. The Challenge Strings can contain irregular spacing that affects word counting: Leading and trailing spaces Multiple consecutive spaces between words Newlines with spaces Empty strings Step-by-Step Approach Step 1: Remove Leading and Trailing Spaces Use regex to eliminate spaces at the start and end of the string: str.replace(/(^\s*)|(\s*$)/gi, ""); Step 2: Reduce Multiple Spaces to Single Space Replace consecutive spaces with a ...
Read MoreExplain in detail about Mark and Sweep algorithm in JavaScript?
Mark and Sweep Algorithm The Mark and Sweep algorithm is JavaScript's primary garbage collection method that identifies objects "which are unreachable" rather than objects "which are no longer needed". This algorithm is an improvement over the Reference-counting algorithm and solves the circular reference problem. How Mark and Sweep Works The algorithm operates in three important steps: Root Identification: The algorithm starts from "roots" - global variables accessible in the code. In browsers, the window object acts as the primary root. Marking Phase: The algorithm traverses from roots to find ...
Read MoreValue of the class attribute node of an element in JavaScript?
JavaScript provides the getAttributeNode() method to retrieve the attribute node of an element as an attribute object. This method returns the attribute node with the specified name, or null if the attribute doesn't exist. Syntax element.getAttributeNode(attributename); It returns the attribute object representing the specified attribute node. To get the actual value, use the value property of the returned object. Example: Getting Class Attribute Value In this example, we have two heading tags with different classes. We'll use getAttributeNode() to retrieve the class attribute and display its value. ...
Read MoreWhat is the importance of _.initial() function in JavaScript?
The _.initial() function is part of the Underscore.js library that returns all elements of an array except the last n elements. It's useful for removing elements from the end of an array without modifying the original array. Syntax _.initial(array, n); Parameters array - The input array from which to extract elements. n (optional) - Number of elements to exclude from the end. Default is 1. Return Value Returns a new array containing all elements except the last n elements. Example: Basic Usage ...
Read MoreHow to add a number and a string in JavaScript?
In JavaScript, when you use the + operator with a number and a string, JavaScript performs concatenation instead of mathematical addition. This is because the presence of a string changes the operation from addition to string concatenation. How It Works JavaScript follows these rules when using the + operator: Number + Number: Mathematical addition String + Number: String concatenation (number is converted to string) Number + String: String concatenation (number is converted to string) Example var a = 5 + 5; ...
Read More