JavaScript(JS) use strict

www.i‮‬giftidea.com

JavaScript use strict

use strict is a directive in JavaScript that enables strict mode, which is a stricter version of the language that helps to avoid common mistakes and bugs.

When you use use strict at the beginning of a script or a function, it enables strict mode for the entire script or function, respectively. Strict mode applies the following restrictions:

  • Prevents the use of undeclared variables
  • Prevents the use of the with statement
  • Disallows duplicate property names in objects
  • Disallows deleting variables, functions, and arguments
  • Disallows assigning values to non-writable properties
  • Disallows the use of eval and arguments as variable names
  • Throws an error when you try to assign a value to a getter-only property
  • Throws an error when you try to define a function in a block statement

Here's an example of how to use use strict at the beginning of a script:

'use strict';

function doSomething() {
  x = 10; // This will throw a ReferenceError in strict mode
}

In this example, we define a function called doSomething and try to assign a value to an undeclared variable x. Since we are using use strict, this will throw a ReferenceError because we are not allowed to use undeclared variables in strict mode.

Using use strict is optional, but it is considered a best practice because it helps to catch errors and enforce better coding habits. However, note that if you are using third-party libraries or frameworks, they may not be written in strict mode and could potentially cause errors when used with strict mode.

Strict Mode in Variable

In JavaScript, when you enable strict mode using the use strict directive, there are a few changes in the behavior of variable declaration and assignment.

First, in strict mode, you must declare a variable before you can use it. If you attempt to assign a value to a variable that has not been declared, strict mode will throw a ReferenceError.

'use strict';

x = 10; // This will throw a ReferenceError in strict mode

In this example, we attempt to assign the value 10 to the variable x without declaring it first. Since we are using strict mode, this will throw a ReferenceError.

Second, in strict mode, you cannot use a variable name that has been reserved for future use. This includes the following variable names:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • yield

If you attempt to use one of these reserved words as a variable name, strict mode will throw a SyntaxError.

'use strict';

let package = 'box'; // This will throw a SyntaxError in strict mode

In this example, we attempt to declare a variable called package, which is a reserved word in strict mode. Therefore, this will throw a SyntaxError.

Finally, in strict mode, duplicate parameter names are not allowed in function declarations or expressions. This helps to avoid potential confusion and bugs when working with functions.

'use strict';

function addNumbers(a, b, a) { // This will throw a SyntaxError in strict mode
  return a + b + a;
}

In this example, we define a function called addNumbers with three parameters, but two of them have the same name (a). Since we are using strict mode, this will throw a SyntaxError.

Strict Mode in Function

When you enable strict mode in JavaScript using the use strict directive, there are a few changes in the behavior of functions. Here are some of the ways that strict mode affects functions:

  1. Strict mode requires all parameter names to be unique.

In non-strict mode, you can define a function with duplicate parameter names:

function myFunction(x, y, x) {
  // code here
}

However, in strict mode, this will cause a syntax error:

'use strict';

function myFunction(x, y, x) {
  // code here
}
// SyntaxError: Duplicate parameter name not allowed in this context
  1. Strict mode prohibits the use of the arguments.callee property.

In non-strict mode, you can use the arguments.callee property to refer to the currently executing function:

function myFunction() {
  if (something) {
    return arguments.callee();
  } else {
    // code here
  }
}

However, in strict mode, using the arguments.callee property will cause a type error:

'use strict';

function myFunction() {
  if (something) {
    return arguments.callee();
  } else {
    // code here
  }
}
// TypeError: 'callee' and 'caller' are restricted function properties and cannot be accessed in this context.
  1. Strict mode prohibits the use of the arguments.caller property.

In non-strict mode, you can use the arguments.caller property to refer to the function that called the current function:

function myFunction() {
  if (something) {
    return arguments.caller();
  } else {
    // code here
  }
}

However, in strict mode, using the arguments.caller property will cause a type error:

'use strict';

function myFunction() {
  if (something) {
    return arguments.caller();
  } else {
    // code here
  }
}
// TypeError: 'callee' and 'caller' are restricted function properties and cannot be accessed in this context.
  1. Strict mode prohibits the use of the eval function.

In non-strict mode, you can use the eval function to evaluate a string of JavaScript code:

function myFunction() {
  eval('console.log("Hello, world!")');
}

However, in strict mode, using the eval function will cause a syntax error:

'use strict';

function myFunction() {
  eval('console.log("Hello, world!")');
}
// SyntaxError: eval cannot be used as a variable name in strict mode

These are just a few of the ways that strict mode affects functions in JavaScript. By enforcing stricter rules for functions, strict mode helps to make your code more reliable and less error-prone.

Things Not Allowed in Strict Mode

Enabling strict mode in JavaScript using the 'use strict' directive restricts certain language features and syntax that are allowed in non-strict mode. Here are some of the things that are not allowed in strict mode:

  1. Implicit global variable declarations

In non-strict mode, if you declare a variable without using the var, let, or const keywords, JavaScript creates an implicit global variable with the same name:

function myFunction() {
  myVariable = 10; // creates an implicit global variable
}

In strict mode, this is not allowed, and assigning a value to an undeclared variable will cause a reference error:

'use strict';

function myFunction() {
  myVariable = 10; // reference error: myVariable is not defined
}
  1. Deleting variables or functions

In non-strict mode, you can delete a variable or function using the delete keyword:

var x = 10;
delete x; // deletes the variable x

In strict mode, deleting a variable or function is not allowed and will cause a syntax error:

'use strict';

var x = 10;
delete x; // syntax error: deleting a variable is not allowed in strict mode
  1. Duplicating parameter names

In non-strict mode, you can declare a function with duplicate parameter names:

function myFunction(x, y, x) {
  // code here
}

In strict mode, this is not allowed and will cause a syntax error:

'use strict';

function myFunction(x, y, x) {
  // syntax error: duplicate parameter name not allowed in strict mode
}
  1. Octal numeric literals

In non-strict mode, you can define a numeric literal using octal notation (starting with a leading zero):

var x = 0777; // octal notation

In strict mode, this is not allowed and will cause a syntax error:

'use strict';

var x = 0777; // syntax error: octal numeric literals are not allowed in strict mode
  1. With statements

In non-strict mode, you can use a with statement to simplify object references:

with (document) {
  var x = getElementById('myElement');
}

In strict mode, using a with statement is not allowed and will cause a syntax error:

'use strict';

with (document) { // syntax error: with statements are not allowed in strict mode
  var x = getElementById('myElement');
}

These are just a few of the things that are not allowed in strict mode. By enforcing stricter rules for the language, strict mode helps to make your code more reliable and less error-prone.