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:
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");
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
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
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
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
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
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
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);
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, ''));
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP