JavaScript Interview Questions

23 Questions
JavaScript

JavaScript

Web DevelopmentFrontendBackend

Question 16

What is strict mode in JavaScript?

Answer:

Strict mode is a way to opt into a restricted variant of JavaScript, which catches common coding errors and unsafe actions such as defining global variables. It can be enabled by adding 'use strict'; at the beginning of a script or function. Strict mode helps in writing secure JavaScript code by eliminating silent errors, improving performance, and preventing certain actions like the use of undeclared variables.

Here is an example demonstrating strict mode:

'use strict';

// This will throw an error because x is not declared
x = 3.14;

// This will throw an error because duplicate parameter names are not allowed
function sum(a, a, c) {
    return a + a + c; // Duplicate parameter name not allowed in strict mode
}

// This will throw an error because deleting a function is not allowed
function myFunction() {}
delete myFunction;

In this example, strict mode catches errors that would otherwise be ignored in non-strict mode, such as using undeclared variables, duplicate function parameters, and deleting a function.

Recent job openings