Skip to main content

Command Palette

Search for a command to run...

Strict Mode in JavaScript

Published
โ€ขView as Markdown
Strict Mode in JavaScript
C

CapsCode: Empowering Innovation Through Education and Technology

CapsCode is a pioneering edtech and web development company committed to transforming the lives of students and businesses alike. Founded with the vision of creating a brighter, tech-driven future, CapsCode empowers students by providing world-class coding education while enabling businesses to achieve their goals with cutting-edge IT solutions.

For Students At CapsCode, we believe that coding is the language of tomorrow. Through engaging and industry-relevant courses, personalized mentoring, and hands-on workshops, we help students develop: Problem-solving skills: Preparing them for real-world challenges. Technical expertise: Equipping them with practical knowledge of coding and web development. Confidence in technology: Making them future-ready for careers in IT and beyond.

We foster an environment where students not only learn to code but also learn to innovate, collaborate, and think critically.

For Businesses CapsCode offers bespoke IT solutions tailored to the unique needs of businesses. From responsive web development to advanced digital marketing strategies, our services are designed to help companies grow their online presence and streamline their operations. Our key offerings include: Custom website and software development E-commerce platform creation Lead generation and digital marketing Professional email and Google Business setup

We act as partners in innovation, helping businesses leverage technology to reach new heights.

Our Mission Our mission is to empower individuals and organizations by bridging the gap between technology and accessibility. We aim to inspire a new generation of coders while helping businesses thrive in a competitive digital landscape.

Why Choose CapsCode? Over 9 years of IT industry expertise. A blend of education and professional IT services under one roof. A commitment to fostering growth, innovation, and success.

Join us in shaping the futureโ€”whether you're a student eager to unlock your potential or a business ready to embrace the possibilities of technology.

Learn. Build. Grow. With CapsCode.

It is new feature included in ES5. It is used to execute the code in "strict mode". we can achieve this by adding 'use strict' in the beginning of your script or function. only the code below the assignment comes under strict mode.

Declaring in the beginning of your script makes it available globally in the code (inside the function as well). Declaring inside the function make it available locally inside the function.

##Strict mode helps out in a couple ways:

  • It catches some common coding bloopers, throwing exceptions.
  • Strict mode eliminates some JavaScript silent errors by changing them to throw errors.
  • It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
  • It disables features that are confusing or poorly thought out.
  • Strict mode makes it easier to write โ€œsecureโ€ JavaScript.

List of features

  • Disallows global variables(object literal also).

    x=20;
    
  • Assignment to a reserved keyword (inc eval & arguments) is not allowed

    var undefined = 5; // throws a TypeError
    var Infinity = 5; // throws a TypeError
    
  • Writing to a read-only property is not allowed:

    var obj = {};
    Object.defineProperty(obj, "x", {value:0, writable: false});
    obj.x = 3.14;  // This will cause an error
    
  • Writing to a get-only property is not allowed:

    var obj = {get x() {return 0} };
    obj.x = 3.14;            // This will cause an error
    
  • Deleting a variable, object, function is not allowed

    delete obj //SyntaxError: Delete of an unqualified identifier in strict mode
    
  • Attempts to delete undeletable properties will throw

    delete Object.prototype //throws error
    
  • Requires all property names in an object literal to be unique

    var x = {x1: "1", x1: "2"} //throws error
    
  • Function parameter names must be unique

    function sum (x, x) {...}//throw error
    
  • Octal numeric literals are not allowed:

    var x = 010; // This will cause an error
    
  • with statement is not allowed:

    with (Math){x = cos(2)}; // This will cause an error
    
  • eval in strict mode does not introduce new variables

    eval ("var x = 2");
    
  • deleting plain names throws error.

    var x
    delete x;
    

###SOME EXAMPLES/ OBSERVATIONS

function func() {
    "use strict"
    y=100; //this will not throw an error till the function is not called
}
"use strict"
function func() {
    y=100; //this will not throw an error till the function is not called
}
function func() {
    myGlobal = 5  //this will NOT give error
  }

  (function() {
    "use strict";
    func()
  })()

  function func_strict() {
    "use strict";
    myGlobal = 5 //this will give error
  }

We hope that this post is helpful for you. If really, Please rate us and let us know your review in the comment.

that's it my dear devs :)

If you like it please give a ๐Ÿ‘

Thank you, CapsCode www.capscode.in