What is difference between test() and exec() in JavaScript?

test(): is used to find a string for a specific pattern. It will return the Boolean value 'true' on finding the given text otherwise, it will return 'false'.
exec(): is used to search a string for a specific pattern, and if it finds it, it'll return the pattern directly; else, it'll return an 'empty' result.

Explain JavaScript bind() method?

bind() method returns a new function, where the value of “this” keyword will be bound to the owner object, which is provided as a parameter.

Code

var vehicleDetails = {
    displayDetails: function(registrationNumber, brandName){
    return this.name+ " , "+ "vehicle details: "+ registrationNumber + " , " + brandName;
  }
}
   
var person1 = {name:  "Ram"};
     
var detailsOfPerson1 = vehicleDetails.displayDetails.bind(person1, "HR26CM4522", "Bullet");

console.log(detailsOfPerson1()); //Returns Ram , vehicle details: HR26CM4522 , Bullet

Explain JavaScript apply() method?

The apply method is similar to the call() method. The only difference is that: call() method takes arguments separately whereas, apply() method takes arguments as an array.

function myMessage(message){
  return this.name  + ", "+message;
} 

var person = {name:  "Ram"};
console.log(myMessage.apply(person, ["How are you!"]));

 

Explain JavaScript call() method?

Call method invokes a method (function) by specifying the owner object.

function print(){
  return "Hello " + this.name;
}
        
var obj = {name: "Ram"};
console.log(print.call(obj)); // Returns "Hello Ram"

call() method allows an object to use the method (function) of another object.

var person = {
  name: "xyz",
  getName: function(){
    return this.name;
  }
}        
var person2 = {name:  "Ram"};

console.log(person.getName()); // Print xyz
console.log(person.getName.call(person2)); // Print Ram

call() accepts arguments:

function myMessage(message){
  return "Hello "+this.name + ", " +message;
}     
var person = {name:  "Ram"};     

console.log(myMessage.call(person, "How are you!")); // Returns "Hello Ram, How are you!"  

What is Self Invoking Function in JavaScript?

Without being requested, a self-invoking expression is automatically invoked (initiated). If a function expression is followed by (), it will execute automatically. A function declaration cannot be invoked by itself.

(function (x) {
    console.log(x);
})(5);

Normally, we declare a function and call it, however, anonymous functions may be used to run a function automatically when it is described and will not be called again. And there is no name for these kinds of functions.

What is this keyword in JavaScript?

The “this” keyword refers to the object that the function is a property of. The value of the “this” keyword will always depend on the object that is invoking the function.

  • In an object method, this refers to the object.
  • Alone, this refers to the global object.
  • In a function, this refers to the global object.
  • In a function, in strict mode, this is undefined.
  • In an event, this refers to the element that received the event.
  • Methods like call(), apply(), and bind() can refer this to any object.

What is Higher Order Function in JavaScript?

Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions.

function higherOrder() {
  return function() {
    return "Hello World";
  }
}      

var x = higherOrder();
console.log(x())   // Returns "Hello World"

What is strict mode in JavaScript?

In ECMAScript 5, a new feature called JavaScript Strict Mode allows you to write a code or a function in a "strict" operational environment. This make debugging lot simpler:

Characteristics of strict mode in javascript

  1. Duplicate arguments are not allowed by developers.
  2. In strict mode, you won't be able to use the JavaScript keyword as a parameter or function name.
  3. The 'use strict' keyword is used to define strict mode at the start of the script. Strict mode is supported by all browsers.
  4. You will not be allowed to create global variables in 'Strict Mode.

Write Anagram Program in JavaScript?

An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example: LISTEN = SILENT and HEART = EARTH

var firstString = "HEART";
var secondString = "EARTH";

console.log(isAnagram(firstString, secondString)); // true

function isAnagram(str1, str2) {
  // Convert to lowercase
  var a = str1.toLowerCase();
  var b = str2.toLowerCase();

  // split characters, sort characters and join them
  a = a.split("").sort().join("");
  b = b.split("").sort().join("");

  return a === b;
}

 

How to use immediate invoking function in JavaScript?

An Immediately Invoked Function ( known as IIFE and pronounced as IIFY) is a function that runs as soon as it is defined.

(function(){ 
  console.log("Hello World");
})();
teststep banner