Found 10878 Articles for Web Development

Do you think JavaScript Functions are Object Methods?

Shubham Vora
Updated on 02-Aug-2022 09:47:35

109 Views

Compared to other languages, JavaScript works differently, especially if you get confused between function and methods. In this tutorial, we will learn whether JavaScript functions are object methods. The example written in this tutorial gives you a better idea about the differences and which one is more convenient for us. What are JavaScript Functions? Functions are generally used to define a particular task that we want to perform. It is a set of instructions, and every function has its block of code. Functions can also have parameters by performing desired operations on those parameters. Syntax The syntax for the ... Read More

How to manipulate JavaScript's Date object?

Shubham Vora
Updated on 20-Oct-2022 08:14:50

1K+ Views

To manipulate JavaScript’s Date object, you can either use get or set method and properties. For example, to set hours or to get minutes in JavaScript. Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time. Here is a list of the methods used to Date and their description. Sr.No Methods & Description 1 date() Returns today's date ... Read More

What are callback functions in JavaScript?

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

282 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");          

How to set current time to some other time in JavaScript?

Daniol Thomas
Updated on 23-Jun-2020 05:05:53

370 Views

You cannot do this with JavaScript since it takes the system time which displays the current date with Date object. However, you can change the current date by changing the timezone as in the following code −Example Live Demo                    var date, offset, nd;          date = new Date();                  document.write("Current: "+date);          utc = date.getTime() + (date.getTimezoneOffset() * 60000);                  // Singapore is GMT+8          offset = 8;                  nd = new Date(utc + (3600000*offset));          document.write("Singapore Time: "+nd.toLocaleString());           Output

How do you access the matched groups in a JavaScript regular expression?

Shubham Vora
Updated on 25-Jul-2022 07:35:18

632 Views

This tutorial will teach us to access the matched groups in JavaScript regular expression. The regular expression is the sequence of the character, also called the RegEx, and it is useful to match specific patterns in the string.There can be more than one match for the specific pattern in the string. To get the occurrence of all matches, we have explained the different methods below in this tutorial.We will also see the various usage of the regular expression in this article.Use the ‘g’ flag while creating the RegexWhen we add ‘g’ as the modifier in the regular expression, it searches ... Read More

How to skip character in capture group in JavaScript Regexp?

Rishi Rathor
Updated on 23-Jun-2020 05:07:57

662 Views

You cannot skip a character in a capture group. A match is always consecutive, even when it contains things like zero-width assertions.ExampleHowever, you can access the matched groups in a regular expression like the following code −                    var str = "Username akdg_amit";          var myReg = /(?:^|\s)akdg_(.*?)(?:\s|$)/g;                    var res = myReg.exec(str);          document.write(res[1]);                      

How can I get a stack trace for a JavaScript exception?

Srinivas Gorla
Updated on 23-Jun-2020 05:12:18

329 Views

To get a JavaScript stack trace, simply add the following in your code. It will display the stack trace −Example Live Demo                    function stackTrace() {             var err = new Error();             return err.stack;          }          console.log(stackTrace());          document.write(stackTrace());                     Stacktrace prints above.     Output

How can I get a JavaScript stack trace when I throw an exception?

Shubham Vora
Updated on 12-Jul-2022 14:10:23

3K+ Views

This tutorial teaches us to get a JavaScript stack trace when we throw an exception. Usually, the developer uses the stack trace to identify the errors while executing the program's code. However, we use the stack trace to debug the program.Using the stack trace, we can get knowledge of any kind of exceptions, such as constructor error, naming error, etc., in our program, and we can correct them.Before we approach analyzing the error using the stack trace, we should know how to stack trace works.How does the call stack trace works?The stack is the primary data structure that follows the ... Read More

How to catch syntax errors in JavaScript?

Sravani Alamanda
Updated on 08-Dec-2022 10:30:13

1K+ Views

In this tutorial, we will learn how to catch syntax errors in JavaScript. In JavaScript, we get will errors if we write any mistakes in code. Like, ReferenceError, i.e., when we call a variable that does not exist in our code. TypeError i.e. when we tried to change the value which is immutable or try to use a variable in an appropriate way. SyntaxError i.e. when we tried to interpret invalid code. We will learn more about this error in detail below. What is Syntax Error? A syntax error is thrown when there is a glitch in ... Read More

How to catch all JavaScript errors?

Saurabh Jaiswal
Updated on 05-Jan-2023 15:22:54

9K+ Views

To catch all JavaScript errors, we can use the window.onerror() method which acts like a global try-catch statement. The onerror event handler was the first feature to facilitate error handling in JavaScript. The error event is fired on the window object whenever an exception occurs on the page. The onerror event handler provides three pieces of information to identify the exact nature of the error − Error message − The same message that the browser would display for the given error URL − The file in which the error occurred Line number− The line number in the given ... Read More

Advertisements