DEV Community

shiva yada
shiva yada

Posted on

Revision 13-03-23

1) What is an immediately invoked function expression in JavaScript?
A soon as function is created it invokes itself doesn’t need to invoke explicitly

(function(value){
    var greet = 'Hello';
    console.log(greet+ ' ' + value); //Hello IIFEs
  })('IIFEs');
Enter fullscreen mode Exit fullscreen mode

2) What is the main difference between push() and concat() method in JavaScript?

  • push() : Adds one or more elements to the end of an array and returns the new length of the array.
    var arr1 = [‘a’, ‘b’, ‘c’];
    var arr2 = [‘d’, ‘e’, ‘f’];
    var arr3 = arr1.push(arr2);
    console.log(arr3); // 4
    console.log(arr1); // [“a”, “b”, “c”, [“d”, “e”, “f”]]
Enter fullscreen mode Exit fullscreen mode
  • concat() : It is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
    var arr1 = [‘a’, ‘b’, ‘c’];
    var arr2 = [‘d’, ‘e’, ‘f’];
    var arr3 = arr1.concat(arr2);
    console.log(arr3); //[“a”, “b”, “c”, “d”, “e”, “f”]
Enter fullscreen mode Exit fullscreen mode

3) What is a callback function in JavaScript?
A callback function is a function that is passed as an argument to another function, and is executed when that function is called. Callback functions are commonly used in asynchronous programming to handle responses from APIs, databases, or other sources of data.

4) What is a pass-by-reference and pass-by-value in the context of JavaScript?

  • Pass by Value: When a primitive data type, such as a number or a string, is passed as an argument, the function receives a copy of that value. Any changes made to the value inside the function do not affect the original value outside of the function.

  • Pass by Reference: When an object or an array is passed as an argument, the function receives a reference to that object or array. Any changes made to the object or array inside the function affect the original object or array outside of the function.

5) What are default parameters in JavaScript?

Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.

function fnName(param1 = defaultValue1, /* … ,*/ paramN = defaultValueN) {
  // …
}
Enter fullscreen mode Exit fullscreen mode

6) How do we use some() and every() method for arrays in JavaScript?

  • some() : The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.
const array = [1, 2, 3, 4, 5];

// Checks whether an element is even
const even = (element) => element % 2 === 0;

console.log(array.some(even));
// Expected output: true
Enter fullscreen mode Exit fullscreen mode
  • every() : The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
const array = [1, 2, 3, 4, 5];

// Checks whether an element is even
const even = (element) => element % 2 === 0;

console.log(array.every(even));
// Expected output: false
Enter fullscreen mode Exit fullscreen mode

7) What is the NaN function in JavaScript?

In JavaScript NaN is short for "Not-a-Number".
The isNaN() method returns true if a value is NaN.
The isNaN() method converts the value to a number before testing it.

8) What is negative indexing in Python?

Negative Indexing is used to in Python to begin slicing from the end of the string i.e. the last.

arr = [10, 20, 30, 40, 50]
print (arr[-1]) //50
print (arr[-2]) //40
Enter fullscreen mode Exit fullscreen mode

9) What are template literals and how do we use them in JavaScript?

  • Template literals allow for string interpolation and multi-line strings.
  • To create a template literal, enclose your string in backticks.
  • The variables are inserted into the string using the ${}. Template literals can also be nested to build more complex strings.

10) What are the parameters taken by the splice() method in JavaScript?

  • start : Zero-based index at which to start changing the array, converted to an integer.
  • deletecount :
    An integer indicating the number of elements in the array to remove from start.

  • item1, …, itemN :

The elements to add to the array, beginning from start.

If you do not specify any elements, splice() will only remove elements from the array.

11) How does the % operator differ for negative numbers in Python and JavaScript?

  • Modulo operator for negative numbers in python: The sign of the second operand will be sign of result.
//-a%b =  add b to a until we reach 0 or positive integer     print(-10%3) //-10 +3 =-7 +3 = -4 +3 = -1+ 3= 2

//a%-b = subtract b to a until we reach  0 or negative integer
print(10%-3) // 10-3= 7-3= 4-3 = 1-3 = -2
Enter fullscreen mode Exit fullscreen mode
  • Modulo operator for negative numbers in Javascript: The sign of first operand will be the sign of result.
// -a%b= - value or 0
console.log(-5%2) //-1

// a%-b = + value or 0
console.log(5%-2) //1

// -a%-b = - value or 0
console.log(-5%-2) //-1
Enter fullscreen mode Exit fullscreen mode

12) What Is Prototype Inheritance in JavaScript?

Prototype inheritance in javascript is the linking of prototypes of a parent object to a child object to share and utilize the properties of a parent class using a child class.

Prototypes are hidden objects that are used to share the properties and methods of a parent class to child classes.

Top comments (0)