Strict Mode in JavaScript

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

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.

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";

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";

[/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/]";

function myFunction()
{
"use strict";
var myOtherString = "This is also a string";
}

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;

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

[/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;

get: function ()
{
return _name;
}
});
}

var employee = new Employee("Mark");

[/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
Рекомендации по теме
Комментарии
Автор

Excellent Tutorial on the benefits of enabling strict mode in JavaScript. Thanks, Venkat

{2021-11-17}

Pareshbpatel
Автор

great sir ..please tell me this is the part of OOJS..

chetanpl
Автор

thanks to author!
does anybody know, why we do not write function Employee in a such way (without _name):
var Employee = function (name)
{
//var _name = name;

Object.defineProperty(this, "name", {
get: function ()
{
//return _name;
return name;
}
});
}
is there a special sense in _name?

oleksandrmonko
welcome to shbcf.ru