Define Custom JavaScript Exceptions

V Jyothi
Updated on 23-Jun-2020 06:33:30

197 Views

To learn how to define and implement custom JavaScript exceptions, you can try to run the following code −Example                                         Click the following to see the result:                          

What are Callback Functions in JavaScript

Nancy Den
Updated on 23-Jun-2020 06:20:32

496 Views

When a function is passed to another function, it is called a callback function. It goes over this function than to call a passed function.ExampleYou can try to run the following code to learn how to work with callback functions −                    var callback = function(myCallback) {             setTimeout(function() {                myCallback();             }, 5000);          };                    document.write("First is displayed");          document.write("Second is displayed");                    callback(function() {              document.write("This is Callback function");          });          document.write("Last is displayed");          

Extract Numbers from a String Using Python

Jayashree
Updated on 23-Jun-2020 06:13:54

741 Views

To extract each digit from a string −>>> str1='a34e 345 bcd 5he 78 xyz' >>> for s in str1: if s.isdigit():print (s) 3 4 3 4 5 5 7 8To extract only integers from a string in which words are separated by space character −>>> str1='h3110 23 cat 444.4 rabbit 11 2 dog' >>> for s in str1.split(): if s.isdigit(): print ((s)) 23 11 2

Return an Object from a JavaScript Function

Abhinaya
Updated on 23-Jun-2020 06:07:20

6K+ Views

To return an object from a JavaScript function, use the return statement, with this keyword.ExampleYou can try to run the following code to return an object from a JavaScipt function − Live Demo                    var employee = {             empname: "David",             department : "Finance",             id : 002,             details : function() {                return this.empname + " with Department " + this.department;             }          };          document.write(employee.details());           Output

Difference Between Custom and Built-in Functions in JavaScript

Anvi Jain
Updated on 23-Jun-2020 06:00:13

722 Views

The custom functions in JavaScript are user-defined functions. JavaScript allows us to write our own functions. The following is the syntax −Syntax     Bult-in functions are functions already provided by JavaScript library, for example, the following are string functions −S. NoMethod & Description1charAt()Returns the character at the specified index.2charCodeAt()Returns a number indicating the Unicode value of the character at the given index.3concat()Combines the text of two strings and returns a new string.4indexOf()Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.ExampleThe following is an example of a built-in ... Read More

Difference Between Functions and Methods in JavaScript

Priya Pallavi
Updated on 23-Jun-2020 05:53:17

2K+ Views

Functions and methods are the same in JavaScript, but a method is a function, which is a property of an object.The following is an example of a function in JavaScript −function functionname(param1, param2){    // code }ExampleThe method is a function associated with an object. The following is an example of a method in JavaScript − Live Demo                    var employee = {             empname: "David",             department : "Finance",             id : 002,             details : function() {                return this.empname + " with Department " + this.department;             }          };          document.write(employee.details());           Output

Difference Between Default and Rest Parameters in JavaScript Functions

Smita Kapse
Updated on 23-Jun-2020 05:49:08

428 Views

Default ParametersThe default parameter came to handle function parameters with ease. You can easily set Default parameters to allow initializing formal parameters with default values. This is possible only if no value or undefined is passed. Example Live Demo                    // default is set to 1          function inc(val1, inc = 1) {             return val1 + inc;          }                    document.write(inc(10, 10));          document.write("");          document.write(inc(10));   ... Read More

Write Regular Expression in JavaScript to Remove Spaces

Anvi Jain
Updated on 23-Jun-2020 05:40:28

281 Views

To remove spaces in JavaScript, you can try to run the following regular expression. It removes the spaces from a string −Example Live Demo                    var str = "Welcome to Tutorialspoint";          document.write(str);                    //Removing Spaces          document.write(""+str.replace(/\s/g, ''));                       Output

JavaScript Regular Expression for Multiple Matches

Nikitha N
Updated on 23-Jun-2020 05:37:40

227 Views

To find multiple matches, write a JavaScript regular expression. You can try to run the following code to implement regular expression for multiple matches −Example                    var url = 'https://www.example.com/new.html?ui=7&demo=one&demo=two&demo=three four',          a = document.createElement('a');          a.href = url;          var demoRegex = /(?:^|[&;])demo=([^&;]+)/g,          matches,          demo = [];          while (matches = demoRegex.exec(a.search)) {             demo.push(decodeURIComponent(matches[1]));          }          document.write(demo);                  

Remove Special Characters from a JavaScript String using Regular Expression

Nishtha Thakur
Updated on 23-Jun-2020 05:34:43

729 Views

To remove all special characters from a JavaScript string, you can try to run the following code −Example                    var str = "@!Welcome to our website$$";          document.write(str);                    // Removing Special Characters          document.write(""+str.replace(/[^\w\s]/gi, ''));                      

Advertisements