Programming Scripts Articles - Page 33 of 37

Match any single character outside the given set.

Rama Giri
Updated on 20-Jan-2020 10:59:37

132 Views

To match any single character outside the given set with JavaScript RegExp, use the [^aeiou] metacharacter.Example           JavaScript Regular Expression                        var myStr = "Welcome!";          var reg = /[^lc]/g;          var match = myStr.match(reg);                   document.write(match);          

How to convert Boolean to Number in JavaScript?

Shubham Vora
Updated on 08-Aug-2022 09:18:13

5K+ Views

In this tutorial, we will convert Boolean to Number in JavaScript. The Boolean is the variable's data type, which is supported by JavaScript like the other programming languages. The Boolean data type contains only two values, true and false. In some cases, programmers must convert the true or false value to the number. For example, using the strict equality operator to compare the Boolean value with the number variable. Here, using different operators, we have three methods to convert the Boolean to a number. Using the Number() Function In JavaScript, the Number() function is useful to convert any variable to ... Read More

Match any string containing a sequence of two to three p's.

Akshaya Akki
Updated on 20-Jan-2020 10:11:41

134 Views

To match any string containing a sequence of two to three p’s with JavaScript RegExp, use the p{2,3} Quantifier.Example           JavaScript Regular Expression                        var myStr = "Welcome 1, 10, 100, 1000, 1000";          var reg = /\d{2,3}/g;          var match = myStr.match(reg);                    document.write(match);          

How to add properties and methods to an object in JavaScript?

V Jyothi
Updated on 23-Jun-2020 08:32:13

286 Views

To add properties and methods to an object in JavaScript, use the prototype property.ExampleYou can try to run the following code to learn how to work with prototype −           JavaScript prototype property                function book(title, author) {             this.title = title;             this.author = author;          }                              var myBook = new book("Amit", "Java");          book.prototype.price = null;          myBook.price = 500;          document.write("Book title is : " + myBook.title + "");          document.write("Book author is : " + myBook.author + "");          document.write("Book price is : " + myBook.price + "");          

How to apply a function simultaneously against two values of the array from right-to-left?

Nitya Raut
Updated on 23-Jun-2020 08:31:18

131 Views

Use the reduceRight() method in JavaScript to apply a function simultaneously against two values of the array from right-to-left as to reduce it to a single value.The following are the parameters −callback − Function to execute on each value in the array.initialValue − Object to use as the first argument to the first call of the callbackExampleYou can try to run the following code to learn how to work with reduceRight() method in JavaScript −           JavaScript Array reduceRight Method                        if (!Array.prototype.reduceRight)         ... Read More

How to apply a function simultaneously against two values of the array from left-to-right?

Manikanth Mani
Updated on 23-Jun-2020 08:28:25

218 Views

Use the reduce() method in JavaScript to apply a function simultaneously against two values of the array from left-to-right as to reduce it to a single value.The following are the parameters −callback − Function to execute on each value in the array.initialValue − Object to use as the first argument to the first call of the callback.ExampleYou can try to run the following code to learn how to work with reduce() method in JavaScript −           JavaScript Array reduce Method                        if (!Array.prototype.reduce) {     ... Read More

How to return a string representing the source for an equivalent Date object?

Daniol Thomas
Updated on 23-Jun-2020 08:07:54

132 Views

To return a string representing the source for an equivalent Date object, use the JavaScript toSource() method. This method returns a string representing the source code of the object.ExampleYou can try to run the following code to return a string representing the source for an equivalent Date object −           JavaScript toSource() Method                        var dt = new Date(2018, 0, 15, 14, 39, 7);          document.write( "Formated Date : " + dt.toSource() );          

How to return the "time" portion of the Date as a string using the current locale's conventions?

Priya Pallavi
Updated on 23-Jun-2020 08:09:03

141 Views

To return the "time" portion of the Date as a string, using the current locale's conventions, use the toLocaleTimeString() method.The toLocaleTimeString method relies on the underlying operating system in formatting dates. It converts the date to a string using the formatting convention of the operating system where the script is running. For example, in the United States, the month appears before the date (04/15/98), whereas in Germany, the date appears before the month (15.04.98).ExampleYou can try to run the following code to return the “time” portion of the Date as a string −           JavaScript toLocaleTimeString ... Read More

How to define a Regular Expression in JavaScript?

Sravani S
Updated on 19-May-2020 11:07:45

208 Views

A regular expression is an object that describes a pattern of characters.The JavaScript RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on the text. A regular expression could be defined with the RegExp () constructor, as follows −var pattern = new RegExp(pattern, attributes); or var pattern = /pattern/attributes;The following are the parameters −pattern − A string that specifies the pattern of the regular expression or another regular expression.attributes − An optional string containing any of the "g", "i", and "m" attributes that specify ... Read More

How to return the "time" portion of the Date as a human-readable string.

Sai Nath
Updated on 23-Jun-2020 08:00:07

129 Views

To return the “time” portion of the Date as a human-readable string, use the toTimeString() method in JavaScript. This method returns the time portion of a Date object in the human-readable form.ExampleYou need to run the following code to return only the time portion of a Date −           JavaScript toTimeString Method                        var dateobject = new Date(2018, 0, 1, 14, 39, 7);          document.write( dateobject.toTimeString() );          

Advertisements