JavaScript use strict

By:    Updated: January 23,2017

Case-sensitivity

Everything is case-sensitive in Javascript; variables, function names, and operators are all case-sensitive, meaning that a variable named "try" is different from a variable named "Try".

Identifiers

An identifier is the name of a variable, function, property, or function argument. Identifi ers may be one or more characters in the following format:

  • The first character must be a letter, an underscore (_), or a dollar sign ($).
  • All other characters may be letters, underscores, dollar signs, or numbers.

Tips: Keywords, reserved words cannot be used as identifiers, such as true, false, and so on.

Strict Mode/Use Strict

ECMAScript 5 introduced the concept of strict mode. Strict mode is a different parsing and execution model for JavaScript, where some of the erratic behavior of ECMAScript 3 is addressed and errors are thrown for unsafe activities. To enable strict mode for an entire script, include the following at the top:

"use strict";

It is a pragma that tells supporting JavaScript engines to change into strict mode. The syntax was chosen specifically so as not to break ECMAScript 3 syntax.

 

You may also specify just a function to execute in strict mode by including the pragma at the top of the function body:

<script type="text/javascript">
function doSomething()
{
    "use strict";
    //function body
}
</script>

 

Statements

Statements in ECMAScript are terminated by a semicolon, though omitting the semicolon makes the parser determine where the end of a statement occurs, as in the following examples:

var sum  = a + b //valid even without a semicolon - not recommended
var diff = a - b; //valid - preferred

Even though a semicolon is not required at the end of statements, it is recommended to always include one.

  • Including semicolons helps prevent errors of omission, such as not fi nishing what you were typing
  • Allows developers to compress ECMAScript code by removing extra white space(if you using the thirdparty software to compress js file to reduce file size, such as yuicompressor)
  • Including semicolons also improves performance in certain situations, because parsers try to correct syntax errors by inserting semicolons where they appear to belong.

Multiple statements can be combined into a code block:

<script type="text/javascript">
//valid, not recommended
if (valinv)
    alert(valinv); //valid, but error-prone and should be avoided

//recommended
if (valinv)
{   
    alert(valinv);
}
</script>

Loosely Typed Variables

ECMAScript variables are loosely typed, meaning that a variable can hold any type of data. Every variable is simply a named placeholder for a value. To define a variable, use the var operator followed by the variable name:

<script type="text/javascript">
/*
 * defines a variable named message that can be used to hold any value
 * Without initialization, it holds the special value "undefined"
 */
var message;

//initialization, assign a value to the variable.
var message = "try.valinv.com";
</script>

Local variable VS Global variable

<script type="text/javascript">
//using the var to define a variable makes it local to the scope in which it was defined
function fsTry()
{
    var message = "valinv"; //local variable
}
fsTry();

//defining a variable inside of a function using var means that the
//variable is destroyed as soon as the function exits
alert(message); //error!
</script>

<script type="text/javascript">
//By removing the var operator, the message variable becomes global.
function fsTry()
{
    message = "valinv"; //global variable
}
fsTry();
//The variable is defined and becomes accessible outside of the function once it has been executed.
alert(message); //"valinv"
</script>

Tips: Although it's possible to define global variables by omitting the var operator, this approach is not recommended. Global variables defined locally are hard to maintain and cause confusion, because it's not immediately apparent if the omission of var was intentional. Strict mode throws a ReferenceError when an undeclared variable is assigned a value.It is recommended to use the following method:

<script type="text/javascript">
var message = ""; //Can be accessed in current script scope(same as global variable)
function fsTry()
{
    message = "valinv"; //global variable
}
fsTry();
alert(message); //"valinv"
</script>

If you need to defi ne more than one variable, you can do it using a single statement, separating each variable (and optional initialization) with a comma:

<script type="text/javascript">
//inserting line breaks and indenting the variables to improve readability
var user    = "flasle",
    wedding = true,
    age     = 29;
</script>
More in Development Center
New on Valinv
Related Articles
Sponsored Links