Found 6710 Articles for Javascript

What is the use of test() method in JavaScript?

vineeth.mariserla
Updated on 01-Jul-2020 10:52:34

1K+ Views

The test() method is a regular expression method. It searches a string for a pattern, and returns true or false, depending on the result. If it encountered the given pattern it returns true, else returns false. It is case sensitive. Let's discuss it in detail.Example-1In the following example, a text named "Tutorix is the best e-learning platform" is given and a pattern "Tu" is checked whether it is present or not. Since the pattern is present the test() method returned true as output.Live Demo Tutorix is the best e-learning platform    var text = document.getElementById("text").innerHTML;    document.getElementById("test").innerHTML = /Tu/.test(text); ... Read More

What is importance of startsWith() method in JavaScript?

vineeth.mariserla
Updated on 01-Jul-2020 10:54:31

141 Views

To know Whether a string starts with a particular character or a string indexOf() method is used. But in the advanced applications, this method is obsolete. So, ES6 has provided us with startsWith() method to perform those advanced tasks.In the following example, the IndexOf() method is used to find whether the string is started with a particular character or not.ExampleLive Demo    var text = 'Tutorialspoint'    document.write(text.indexOf('T') === 0); OutputtrueIn the following example, instead of indexOf() method,  startsWith() method is used to find whether the string is started with a particular string or not.ExampleLive Demo ... Read More

How to remove existing HTML elements in JavaScript?

Lokesh Yadav
Updated on 06-Sep-2023 13:57:02

47K+ Views

This article discusses about how to delete an existing element in JavaScript. To remove the existing HTML elements, we first need to select the element to be deleted from the document. Then, use the methods like remove() and removeChild() in JavaScript to delete the elements from the document. We’ll discuss about both methods in this article. Using the remove() methodThe remove() method of JavaScript will remove the element from the document. The syntax for the remove method is shown below. Obj.remove(); Using removeChild() methodThe removeChild() method of JavaScript will remove the element from the document. The syntax for the ... Read More

How to add a new element to HTML DOM in JavaScript?

Lokesh Yadav
Updated on 02-Sep-2023 02:08:51

74K+ Views

In this article we are going to discuss how to add a new element to HTML DOM in JavaScript. The Document object provides a method createElement() to create an element and appendChild() method to add it to the HTML DOM. Following are the steps involved in creating HTML DOM − Step 1 − To insert an element into HTML DOM, firstly, we need to create an element and append to HTML DOM. The document.createElement() is used to create the HTML element. The syntax to create an element is shown below. document.createElement(tagName[, options]); Where, tagName is the name of ... Read More

What is the importance of '/i' flag in JavaScript?

Manisha Patil
Updated on 04-Aug-2022 11:35:56

455 Views

Let us understand first about regular expressions before jump into I flag. A string of characters that creates a search pattern is called a regular expression. The search pattern can be applied to text replacement and search operations. A regular expression might be a simple pattern with one letter or a more complex one. Text search and replace operations of every kind can be carried out using regular expressions. The literal notation and the constructor are the two methods for creating a RegExp object. The literal notation consists of a pattern among both two slashes and optionally flags beyond ... Read More

What is the importance of '/g' flag in JavaScript?

Manisha Patil
Updated on 04-Aug-2022 11:34:12

581 Views

Let us understand first about regular expressions before jumping into the /g flag. A string of characters that creates a search pattern is called a regular expression. The search pattern can be applied to text replacement and search operations. A regular expression might be a simple pattern with one letter or a more complex one. Text search and replace operations of every kind can be carried out using regular expressions. The literal notation and the constructor are the two methods for creating a RegExp object. The literal notation consists of a pattern among both two slashes and optionally flags ... Read More

How to find the inner height and inner width of a browser window in JavaScript?

Lokesh Yadav
Updated on 08-Dec-2022 07:31:59

596 Views

In this article we are going to learn how to find the inner height and inner width of a browser window using JavaScript. JavaScript defines window object methods and properties. The properties include inner height and inner width, which helps us to calculate the height and width of a window’s content area. There are two ways to calculate the inner height and inner width of a window. Using innerHeight and innerWidth properties − To get the inner height and inner width JavaScript has provided some properties such as window.innerHeight and window.innerwidth respectively. Using these properties, it is easy to ... Read More

How to get the pixel depth and color depth of a screen in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

557 Views

Javascript window object has provided many methods to get various kinds of information regarding the browser.  It has provided screen.colorDepth and screen.pixelDepth to get the color depth and pixel depth of browser screen respectively. Let's discuss them individually.Color depthThe window object has provided screen.colorDepth method to return the color depth. Color depth is nothing but the number of bits used to display one color. All modern computers use 24 bit or 32 bit hardware for color resolution.ExampleLive Demo document.getElementById("depth").innerHTML = "Screen color depth is " + screen.colorDepth; OutputScreen color depth ... Read More

How to remove all the elements from a map in JavaScript?

vineeth.mariserla
Updated on 01-Jul-2020 07:26:13

1K+ Views

The map is basically a collection of elements where each element is stored as a Key, value pair. It can hold both objects and primitive values as either a key or a value. When we iterate over the map object it returns the key, value pair in the same order as inserted. The map has provided a method called map.clear() to remove the values inside a map. This method will remove every key/value pair and make the map totally empty.syntaxmap.clear(obj);map.obj() takes an object as a parameter and removes each and every value so as to make it empty.Example-1In the following example, a map is created ... Read More

How to return an array whose elements are the enumerable property values of an object in JavaScript?

vineeth.mariserla
Updated on 01-Jul-2020 08:08:49

244 Views

We can use some logical methods to get the values and also keys from an object, but those methods will not return the values as an array, which is very useful in many cases. Javascript has provided Object.values() method to get an array whose elements are the enumerable property values of an object.syntaxObject.values(obj);This method takes an object as an argument and returns an array whose elements are nothing but the property values of the object.Example-1In the following example, an object is sent through the method object.values() and the property values were displayed as an array.Live Demo    var obj = ... Read More

Advertisements