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 85 of 589
What is the importance of str.padStart() method in JavaScript?
The padStart() method in JavaScript pads a string at the beginning to reach a specified length. It's particularly useful for formatting numbers, creating aligned text, and adding prefixes with consistent string lengths. Syntax string.padStart(targetLength, padString) Parameters targetLength: The desired length of the resulting string after padding. padString (optional): The string to use for padding. Defaults to a space character if not provided. Return Value Returns a new string padded at the beginning. The original string remains unchanged. Basic Example ...
Read MoreIn how many ways can we find a substring inside a string in javascript?
JavaScript provides several methods to find a substring inside a string. The most commonly used approaches are indexOf(), includes(), search(), and regular expressions with match(). Using indexOf() Method Syntax string.indexOf(substring, startPosition) The indexOf() method returns the index of the first occurrence of the substring. If the substring is not found, it returns -1. This method is case-sensitive. Example var company = "Tutorialspoint"; document.write("Index of 'point': " + company.indexOf('point')); document.write(""); document.write("Is 'Tutor' present: " + (company.indexOf('Tutor') !== ...
Read MoreHow to get the code name and product name of the browser in JavaScript?
When a user accesses a web application, the JavaScript navigator object contains details about the browser they are currently using. Since each browser functions differently in how it interprets JavaScript, the navigator object helps in adapting your application to the user's browser preferences. The browser engine or product name can be accessed in JavaScript using the navigator.product property. However, in modern browsers, navigator.product always returns "Gecko" for compatibility reasons, regardless of the actual browser being used. Navigator.product Property Syntax navigator.product Example 1: Getting Browser Product Name This example demonstrates how to access ...
Read MoreHow to get the protocol and page path of the current web page in JavaScript?
In JavaScript, you can retrieve both the protocol (like https:// or http://) and the page path of the current webpage using the window.location object and URL constructor. These properties are essential for web development tasks like URL manipulation and navigation. Getting the Protocol The protocol specifies the data transfer method used by the URL. Use window.location.protocol or URL.protocol to retrieve it. Syntax protocol = window.location.protocol; // or protocol = new URL(url).protocol; Using window.location.protocol Protocol Example ...
Read MoreHow to return an array whose elements are the enumerable property values of an object in JavaScript?
We can use various methods to get values and keys from an object, but those methods will not return the values as an array, which is very useful in many cases. JavaScript provides the Object.values() method to get an array whose elements are the enumerable property values of an object. Syntax Object.values(obj); This method takes an object as an argument and returns an array whose elements are the property values of the object. Parameters obj: The object whose enumerable property values are to be returned. Return Value An array containing the ...
Read MoreHow to get the pixel depth and color depth of a screen in JavaScript?
The JavaScript window object provides methods to get screen display information through the screen object. Two important properties are screen.colorDepth and screen.pixelDepth which return the color depth and pixel depth of the browser screen respectively. Color Depth The screen.colorDepth property returns the number of bits used to display one color on the screen. Modern computers typically use 24-bit or 32-bit hardware for color resolution, where: 24-bit color: Supports 16.7 million colors (True Color) 32-bit color: Includes an alpha channel for transparency Syntax screen.colorDepth Example ...
Read MoreWhat is the importance of '/g' flag in JavaScript?
The /g flag in JavaScript regular expressions stands for "global" and is essential for finding all matches in a string rather than stopping after the first match. Without this flag, regex methods will only find the first occurrence of a pattern. Understanding Regular Expressions Regular expressions are patterns used to match character combinations in strings. They can be created in two ways: Literal notation: Uses forward slashes with optional flags after the second slash (e.g., /pattern/g). This is preferred when the pattern remains constant. Constructor function: Uses new RegExp("pattern", "flags"). This is useful when the pattern ...
Read MoreWhat is the importance of '/i' flag in JavaScript?
The /i flag in JavaScript is a regular expression modifier that enables case-insensitive pattern matching. By default, regular expressions are case-sensitive, meaning they distinguish between uppercase and lowercase letters. Understanding Regular Expressions Regular expressions are patterns used to match character combinations in strings. They can be created using two methods: Literal notation: Uses slashes to enclose the pattern (/pattern/flags). Best for static patterns that won't change during execution. Constructor method: Uses the RegExp constructor (new RegExp("pattern", "flags")). Ideal when the pattern needs to be dynamic or constructed at runtime. ...
Read MoreWhat 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 More