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

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

Write Regular Expression in JavaScript to Remove Spaces

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

265 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

200 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

718 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, ''));                      

Get Stack Trace for JavaScript Exception

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

480 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

Skip Character in Capture Group in JavaScript Regexp

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

946 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]);                      

Set Current Time to Another 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

Intersect Two Lists in C#

George John
Updated on 22-Jun-2020 15:51:21

10K+ Views

Firstly, set two lists.List val1 = new List { 25, 30, 40, 60, 80, 95, 110 }; List val2 = new List { 27, 35, 40, 75, 95, 100, 110 };Now, use the Intersect() method to get the intersection between two lists.IEnumerable res = val1.AsQueryable().Intersect(val2);Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List val1 = new List { 25, 30, 40, 60, 80, 95, 110 };       List val2 = new List { 27, 35, 40, 75, 95, 100, 110 };       IEnumerable res = val1.AsQueryable().Intersect(val2);       Console.WriteLine("Intersection of both the lists...");       foreach (int a in res) {          Console.WriteLine(a);       }    } }OutputIntersection of both the lists... 40 95 110

What Are Shell Commands

Kristi Castro
Updated on 22-Jun-2020 15:49:49

11K+ Views

The shell is the command interpreter on the Linux systems. It the program that interacts with the users in the terminal emulation window. Shell commands are instructions that instruct the system to do some action.Some of the commonly used shell commands are −basenameThis command strips the directory and suffix from filenames. It prints the name of the file with all the leading directory components removed. It also removes a trailing suffix if it is specified.Example of basename is as follows −$ basename country/city.txtThis gets the name of the file i.e. city which is present in folder country.city.txtcatThis command concatenates and ... Read More

Advertisements