DEV Community

Saravanan
Saravanan

Posted on

Revision 15-03-2023

1. How can you copy objects in JS?

There are three ways to copy an object in JS. They are

  • using spread syntax.
  • using Object.asssign() method.
  • using JSON.stringify() and JSON.parse() methods.

Example:

const person = {
    firstName: 'John',
    lastName: 'Doe'
};

// using spread ...
let p1 = {
    ...person
};

// using  Object.assign() method
let p2 = Object.assign({}, person);

// using JSON
let p3 = JSON.parse(JSON.stringify(person));
Enter fullscreen mode Exit fullscreen mode

2. What is the difference between a named function and an anonymous function in JS?

A named function is a function declaration and an anonymous function is a function expression. The named function is hoisted but, anonymous functions are not. Anonymous functions are mostly used as a callback functions. Named function uses function keyword and anonymous function uses arrow syntax.

function my_function() {
    console.log("named function");
}

my_function(); // logs "named function"

(() => {
    console.log("anonymous function");
})(); // logs "anonymous function"
Enter fullscreen mode Exit fullscreen mode

3. How to check if a string contains a substring in JS?

  • String.prototype.includes function returns a boolean value true if the substring is present.
  • String.prototype.indexOf function returns -1 if the substring is not present and a positive index value if present.
console.log("abcdefgh".includes("gh")); // logs true
console.log("abcdefgh".indexOf("zz")); // logs -1
Enter fullscreen mode Exit fullscreen mode

4. What is the difference between indexOf() and lastIndex() methods in JS?

The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value.

5. Is JS static or dynamically typed language?

JS is a dynamically typed language.

6. How can you iterate over elements of an array without using for and while loop in JS?

Using Array.prototype.forEach function helps to iterate over an array without using the traditional for and while loop.

[1, 2, 3, 4, 5].forEach((val) => {
    console.log(val);
}); // logs 1 2 3 4 5 in a newline.
Enter fullscreen mode Exit fullscreen mode

7. What is a Cartesian product in SQL?

In SQL cartesian product is cross-join which gives the cross product of two tables.

SELECT *
FROM COLOR
CROSS JOIN SIZE;
Enter fullscreen mode Exit fullscreen mode
COLOR
RED
BLUE
SIZE
LARGE
MEDIUM
SMALL
COLOR SIZE
RED SMALL
RED MEDIUM
RED LARGE
BLUE SMALL
BLUE MEDIUM
BLUE LARGE

8. In which array methods can we use negative indexes and how can we use them?

JS does not support a negative index to access the item directly. The way to access the item using a negative index is Array.prototype.at method. All the array function which has an index as a parameter also accepts a negative index.

console.log([1, 2, 3, 4][-1]); // logs undefined
console.log([1, 2, 3, 4].at(-1)) // logs 4
Enter fullscreen mode Exit fullscreen mode

9. What is destructuring in JS?

Destructuring is a feature introduced in ECMAScript 6 (ES6) that allows you to extract values from arrays or properties from objects and bind them to variables in a concise and readable way.

const obj = { a: 1, b: 2 };
const { a, b } = obj; // a is 1 and b is 2
Enter fullscreen mode Exit fullscreen mode

10. What is the expected output when giving 'None' as a parameter while using the filter function on a list in Python?

When None is passed as a parameter to the filter() function it returns an iterator that filters out 'falsy' values such as empty list, string numerical zero, etc, or having the length as 0.

my_vals = [1, False, "", {}, 0, "word"]
my_vals = filter(my_vals) # my_vals is [1, "word"] 
Enter fullscreen mode Exit fullscreen mode

11. What is the actual use case of JSON.stringify() and JSON.parse()?

  • It helps to store objects in localStorage and sessionStorage.
  • Used while making API requests.
  • Store objects in a flat file.

Top comments (0)