Activating JavaScript Strict Mode in Individual Functions

Telegram Join Our Telegram Channel

You can also enable string mode for a particular function by adding  ‘use strict’; or “use strict”; at the beginning of the function.

Syntax:

functionfunctionName() {    ‘use strict’;    // function body}// orfunctionfunctionName() {    "use strict";    // function body}

This statement should be the first statement of the function excluding comments.

Adding ‘use strict’; or “use strict”; at the beginning of a function has a local scope which means all the code in that script is executed usually and the code inside the function will only execute in strict mode.

Example:

functionName() {  // Function without strict mode    name = "Rack"    console.log("Name is: "+ name) // No error here}functionAge() {  // Function with strict mode    "use strict";    age = "22"    console.log("Age is: "+ age) // Error here}Name()Age()

Output:

Name is: RackC:\Users\ag290\OneDrive\Desktop\journalDev-2023\january\strict mode\app.js:8    age = "22"        ^ReferenceError: age is not defined
Telegram Join Our Telegram Channel

Leave a Reply

Your email address will not be published. Required fields are marked *

Telegram Join Our Telegram Channel

Most Viewed

Monthly Best Selling Templates

Check the latest products added to the marketplace. Fresh designs with the finest HTML5 CSS3 coding.