Javascript Articles - Page 602 of 671

What is function overloading in JavaScript?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:21

2K+ Views

JavaScript does not support Function Overloading. The following shows function overloading − function funcONE(x,y) { return x*y; } function funcONE(z) { return z; } The above will not show an error, but you won't get desired results. On calling, // prints 5 funcONE(5); // prints 5, not 30 funcONE(5,6); JavaScript does not support function overloading natively. If we will add functions with the same name and different arguments, it considers the last defined function.

What is the difference between default and rest parameters in JavaScript functions?

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

401 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

What is the 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

How to 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

What is the difference between custom and built-in functions in JavaScript?

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

690 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

Do you think JavaScript Functions are Object Methods?

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

219 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

3K+ 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

483 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

569 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

887 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

Advertisements