Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Strict Mode in JavaScript
Play lesson

JavaScript Tutorial - Strict Mode in JavaScript

4.0 (0)
13 learners

What you'll learn

This course includes

  • 12.3 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

Link for all dot net and sql server video tutorial playlists http://www.youtube.com/user/kudvenkat/playlists Link for slides, code samples and text version of the video http://csharp-video-tutorials.blogspot.com/2015/02/strict-mode-in-javascript.html Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help. https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1 Just like Java and C#, JavaScript is also an object oriented programming language. However, JavaScript is not very strict in reporting or throwing errors. Let us understand what this statement means with an example. In C#, if you assign a value to a variable that is not declared, you get an error stating - The name 'myString' does not exist in the current context. myString = "This is a string"; Where as the same code in JavaScript does not raise any error. JavaScript automatically creates a global variable with name myString. This kind of a behaviour may lead the developer to make more mistakes. myString = "This is a string"; document.write(myString); Output : This is a string Just like Java & C#, if you want JavaScript also to throw an error, then use Strict Mode. How to enable strict mode in JavaScript : Just add "use strict" statement in your script file as shown below. When you run the page, you will now get an error stating - Variable undefined in strict mode. To see the error in google chrome, please go the console window in developer tools. [script type="text/javascript"] "use strict"; myString = "This is a string"; document.write(myString); [/script] Since "use strict" is specified at the top of the JavaScript file, strictness will be enforced across the entire script file. How to enforce JavaScript strictness in a specific function : Just add "use strict" statement in the function as shown below. In this example, strictness is enforced only in myFunction(). [script type="text/javascript"] myString = "This is a string[br/]"; document.write(myString); function myFunction() { "use strict"; var myOtherString = "This is also a string"; document.write(myOtherString); } myFunction(); [/script] Output : This is a string This is also a string Let us look at another example : In C# if you assign a value to a read-only property you get an error. For example, the following C# code would raise an error stating - Property or indexer 'Demo.Employee.Name' cannot be assigned to -- it is read only. public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Employee employee = new Employee("Mark"); employee.Name = "Mark M"; } } public class Employee { private string _name; public Employee(string name) { this._name = name; } public string Name { get { return this._name; } } } We discussed JavaScript properties in Part 58 of JavaScript tutorial. In JavaScript, if you assign a value to a read-only property, JavaScript silently fails without raising an error. [script type="text/javascript"] var Employee = function (name) { var _name = name; Object.defineProperty(this, "name", { get: function () { return _name; } }); } var employee = new Employee("Mark"); // name is readonly property. It is an error to assign a value to a read-only property // JavaScript silently fails the following line without raising an error employee.name = "Mark M"; document.write(employee.name); [/script] If you want JavaScript to raise an error instead of failing silently, use JavaScript strict mode. The code below raises an error stating - Assignment to read-only properties is not allowed in strict mode. [script type="text/javascript"] "use strict"; var Employee = function (name) { var _name = name; Object.defineProperty(this, "name", { get: function () { return _name; } }); } var employee = new Employee("Mark"); employee.name = "Mark M"; document.write(employee.name); [/script] ECMAScript version 5 introduced strict mode to JavaScript. With strict mode on it is easier to detect JavaScript silent errors as they would throw an error now. This makes debugging much easier and the chances of developers making mistakes is reduced. Most modern browsers support strict mode. For the list of most important restrictions that apply in strict mode, please refer the following MSDN article https://msdn.microsoft.com/en-us/library/ie/br230269(v=vs.94).aspx

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere