Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to enable JavaScript in my browser ?
Next article icon

What is Strict Mode and how to enable it in JavaScript ?

Last Updated : 08 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The strict mode of JavaScript, introduced in ECMAScript 5, is a mechanism to opt into a restricted version of JavaScript." Strict mode modifies the regular JavaScript semantics in the following ways: Some JavaScript silent errors are eliminated by converting them to throw errors.

How to enable strict mode in JavaScript:

Use "use strict" in front of the code where strict mode is necessary to enable strict mode. The strict mode can be enabled by simply stating it at the top of your script or in the required function. When a JavaScript engine encounters this directive, it begins to parse the code in a specific mode where error catching becomes easier.

 

Advantages of using strict mode:

  • The strict mode eliminates silent errors by changing them into thrown errors. so, it makes debugging easier
  • When the delete operator is used where it is invalid strict mode throws errors.
  • When the variables are used without being declared or we call them "accidental globals", strict mode throws an error.
  • It doesn't allow duplicate property names or parameter values.
  • .When we do not use strict mode and our "this" operator points reference to null or undefined values it doesn't give an error but enabling the strict mode raises an error.
  • Code written in strict mode can sometimes be made to execute quicker than code written in a non-strict manner.

The strict mode doesn't allow the following instructions:

  • We cannot directly use variables without declaring the variable.
  • Parameters/arguments duplicates are not allowed.
  • Deletion function not allowed.
  • The words 'eval' and 'arguments' cannot be used as a variable.
  • The 'with' statement is not allowed.
  • Deleting a property that is undeletable.
  • Writing to a read-only property.

Examples:

Demonstration without using the strict mode:  In the below example we are assigning a variable without declaring it and then printing it.

JavaScript
a = 1; console.log(a); 

Output:

 

Demonstration with using the strict mode: In the below example we are assigning a variable without declaring it and then printing it. we enable strict mode at the top of the code. The error message is displayed as we use strict mode.

JavaScript
"use strict"; a = 1; console.log(a); 

Output: 

Demonstration an example with or without using the strict mode: In the below example one part of the javascript code is running under strict mode and the other is running in non-strict mode.

JavaScript
// This will not cause an error a = 10; . console.log(a); withUsingStrict();  function withUsingStrict() {   "use strict";   b = 20; // This will cause an error   console.log(b); } 

Output:

 


Next Article
How to enable JavaScript in my browser ?
author
isitapol2002
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

  • What is JavaScript Strict mode and how can we enable it ?
    JavaScript is a forgiving language as it ignores developers' or programmers' silly mistakes or errors in code like termination of the statement, variable declaration, the wrong data type of variable, hoisting issues, and many more. Sometimes these errors give unusual results which difficult for prog
    5 min read
  • How to Enable JavaScript on a MacOS?
    JavaScript is a programming language used to create dynamic content on web pages, such as animations or real-time updates, without needing to refresh the page. Enabling JavaScript in your browser is essential for a smooth online experience, like automatic updates on Facebook or Twitter timelines. In
    2 min read
  • How to Enable JavaScript on Android ?
    JavaScript is the world's most popular lightweight, interpreted compiled programming language. It is also known as a scripting language for web pages. It is well-known for the development of web pages, many non-browser environments also use it. Therefore nowadays it is essential for users to have Ja
    2 min read
  • How to Disable Input in JavaScript ?
    Disabling input fields in JavaScript is a common task when you want to restrict users from interacting with specific form elements. This can be useful in various scenarios, such as preventing modifications to fields that are conditionally locked or ensuring certain inputs are controlled programmatic
    2 min read
  • How to enable JavaScript in my browser ?
    We will explore the process of enabling JavaScript in web browsers to ensure seamless functionality of applications and websites. JavaScript serves as a fundamental component of modern web development, facilitating dynamic interactions and enhanced user experiences. However, encountering issues wher
    2 min read
  • How to check whether a given string is an absolute URL or not in JavaScript?
    We will learn how to return true if the given string is an absolute URL in JavaScript. There are two types of URLs either relative or absolute. Absolute URL: It is a URL that contains all the information that is important for locating the resources. The thing that is included in the absolute URL is
    3 min read
  • What does OR Operator || in a Statement in JavaScript ?
    JavaScript is a dynamic programming language that allows developers to write complex code with ease. One of the fundamental concepts in JavaScript is the use of operators, which are symbols that perform operations on one or more values. One such operator is the || (logical OR) operator, which can be
    6 min read
  • What Are Access Modifiers In JavaScript ?
    Access modifiers in JavaScript play a significant role in controlling the visibility and accessibility of class members. Although JavaScript is not traditionally an object-oriented programming (OOP) language, the introduction of classes and access modifiers in ECMAScript 6 (ES6) allowed developers t
    4 min read
  • Difference between != and !== operator in JavaScript
    != operatorThe inequality operator (!=) is the logical opposite of the equality operator. It means "Not Equal" and returns true whereas equality would return false and vice versa. Like the equality operator, the inequality operator will convert data types of values while comparing. For example 1 !=
    2 min read
  • JavaScript Strict Mode
    Strict Mode in JavaScript is a feature that helps catch common coding mistakes and implement a stricter set of rules to improve code quality. It eliminates silent errors, prevents the use of unsafe actions, and improves performance optimization by JavaScript engines. Use strict directiveThe "use str
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences