DEV Community

CNavya21
CNavya21

Posted on

Revision 11-03-2023

1. What happens when you try to divide a number by 0 in JavaScript?

  • If we divide with positive number by zero then it will return infinity.
  • If we divide with zero by zero it will returns undefined.
  • If we divide with negative number by zero then it will returns -infinity.

2. What is the difference between a traditional for loop and "for each" loop in JavaScript?

  • The for loop that will use three optional expressions to implement the repeated execution of a code block.

Syntax

for (initialexpression; condition; updateexpression) {
    // for loop body
}
Enter fullscreen mode Exit fullscreen mode
  1. initialExpression : it declares variables and executes only once.
  2. condition : it checks the condition, if the condition is false then loop will terminate and the condition is true then block of code inside of the for loop is executed.
  3. updateexpression : updates the value of initialexpression when the condition is true.

Example

const n = 3;
for (let i = 1; i <= n; i++) {
    console.log(`I love JavaScript.`);
}
# output: I love JavaScript.
          I love JavaScript.
          I love JavaScript.
Enter fullscreen mode Exit fullscreen mode
  • The forEach() method calls a function (a callback function) for each element in an array.It doesn't execute for an empty array.

Syntax

array.forEach(function(currentValue, index, arr), thisValue)
Enter fullscreen mode Exit fullscreen mode
  1. currentValue : It takes the current value of element.
  2. index : It takes the index of current value of element.
  3. arr : The array of current element.
  4. thisValue : It takes defaultly undefined.A value passed to the function as its this value.

Example

let students = ['John', 'Sara', 'Jack'];
students.forEach(myFunction);
function myFunction(item, index, arr) {
    arr[index] = 'Hello ' + item;
}
console.log(students);
# output: ["Hello John", "Hello Sara", "Hello Jack"]
Enter fullscreen mode Exit fullscreen mode

3. What is the difference between Null and undefined in JavaScript?

  • null is a special value that represents an empty or unknown value.

Example

var test1 = null;
console.log(test1);
# output: null
Enter fullscreen mode Exit fullscreen mode
  • Undefined means a variable has been declared, but the value of that variable has not yet been defined.

Example

var test2;
console.log(test2);
# output : undefined
Enter fullscreen mode Exit fullscreen mode

4. How to activate the strict mode and what is its use in JavaScript?

  • Strict mode is declared by adding "use strict"; to the beginning of a script or a function.

Advantages

  • Strict mode eliminates JavaScript silent errors by changing them to throw errors.
  • Fixes mistake that make it difficult for JavaScript engine to perform better.
  • Make code run faster than identical code that's not in strict mode.

5. What happens when you don't have a return statement in a function in JavaScript?

  • The function will automatically returns undefined.

6. What is the UNIQUE constraint in SQL and how is it different from PRIMARY KEY?

  • UNIQUE constraint checks for all the values in a column or set of columns are different or not.
  • Both the UNIQUE and PRIMARY KEY constraints provide a permission for uniqueness for a column or set of columns.
  • A PRIMARY KEY constraint automatically has a UNIQUE constraint.
  • You can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

7. What is the difference between PRIMARY KEY and FOREIGN KEY?

  1. Primary key:
    • A primary key is used to ensure that data in the specific column is unique. A column cannot have NULL values.
    • Only one primary key is allowed in a table.
    • It does not allow NULL values.

2.Foreign key:

  • A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables.
  • Whereas more than one foreign key are allowed in a table.
  • It contains NULL values.

8. What is the unshift() method in JavaScript?

  • It adds one or more elements in beginning of the array and returns the new array as output.

Example

const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));
// Expected output: 5

console.log(array1);
// Expected output: Array [4, 5, 1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

9. In JavaScript string methods, what is the difference between charAt() and charCodeAt(), and what will happen if no arguments are provided with these methods?

  • charAt() method returns the character at the specified index.

Syntax

string.charAt(index)
Enter fullscreen mode Exit fullscreen mode

Example

let text = "HELLO WORLD";
let letter = text.charAt(text.length-1);
#output: D
Enter fullscreen mode Exit fullscreen mode
  • charCodeAt() returns Unicode value of the character at the specified Index.

Syntax

string.charCodeAt(index)
Enter fullscreen mode Exit fullscreen mode

Example

let text = "HELLO WORLD";
let code = text.charCodeAt(0);
#output: 72
Enter fullscreen mode Exit fullscreen mode
  • If we don't pass any argument, then it will return the character and Unicode value of first character in the string respectively.

10. What is the difference between slice() and splice() in JavaScript?

  • slice() : This method is used to get a new array by selecting a sub-array of a given array.

Syntax

string.slice(start, end)
Enter fullscreen mode Exit fullscreen mode

Example

let text = "Hello world!"; 
let result = text.slice(3, 8);
#output: lo wo
Enter fullscreen mode Exit fullscreen mode
  • splice() : This method is used to add/remove an item from the given array.

Syntax

splice(start, deleteCount, item1, item2, itemN)
#start is the index from where you want to modify the array
#deleteCount is an optional parameter and refers to the number of elements to remove.
Enter fullscreen mode Exit fullscreen mode

Example

let fruits = ['mango', 'banana, 'apple', 'kiwi', 'orange']
fruits.splice(2)
#output: ['apple', 'kiwi', 'orange']
Enter fullscreen mode Exit fullscreen mode

11. How do we ensure any package updates for any older dependencies get reflected in the package.json file?

  • No, It won't reflect on the package.json file when we update the npm version.It remains the same version in the dependencies.

12. How is atomicity achieved in a relational database?

  • The entire transaction takes place at once or doesn’t happen at all.

13. What is the difference between the "for-each" loop and map method in JavaScript?

  • The map() method returns a new array, whereas the forEach() method does not return a new array.
  • The map() method is used to transform the elements of an array, whereas the forEach() method is used to loop through the elements of an array.
  • The map() method can be used with other array methods, such as the filter() method, whereas the forEach() method cannot be used with other array methods.

Top comments (0)