DONATE  
MENU
HTML
AJAX
JSON
MYSQL
JAVASCRIPT
PHP
CSS

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Miscellaneous » JS Strict Mode
Share Tweet WhatsApp

JS Strict Mode

JavaScript strict mode is a recent add on or feature that forces you to write syntactically correct programs that can be executed in any browser without your code having to fail. This is because JavaScript strict mode forces you to ensure your codes are syntactically correct before they can be executed. For example, in JavaScript, you should declare a variable with the var keyword before using it but then, in a non - strict mode, you can use a variable without it being declared. This is not allowed in the strict mode as your program won't execute. Non - strict mode
<script>
myName = "Joseph Omamode"; //variable not declared
alert(myName);
</script>

Copy PRACTICE IT

Strict mode
<script>
"use strict";
myName = "Joseph Omamode"; //variable not declared
alert(myName);
</script>

Copy PRACTICE IT

Above, the program won't be executed because you are on strict mode and a variable wasn't declare before use, which is a syntactical error. You get to understand variables' scope under strict mode. JavaScript Classes Are Supported In Strict Mode Only JavaScript Classes are supported in strict mode only. Classes were introduced in ECMAScript-5 and they are just like object prototypes from which different objects can be created. Just as JavaScript uses prototypes in creating objects, most programming languages use Classes to do this. Therefore, Classes were introduce so that object oriented programmers in other languages can easily find themselves creating objects in JavaScript but then, Classes are only supported in strict mode for now. The below won't execute because it is not strict mode. Nevertheless, this may change soon or already changed at your time of going through this tutorial.
<script>
    class Car{
constructor(carName){
document.write(carName+" started...")
this.name = carName;
} //constructor function

stop(){
document.write("<br/>"+this.name+" stopping...stopped.")
} //A method
}

//Creating Object
var toyota = new Car("Toyota Camry");
//Using Method Of Object
toyota.stop();
</script>

Copy PRACTICE IT

Classes are supported in strict mode only.
<script>
"use strict";
    class Car{
constructor(carName){
document.write(carName+" started...")
this.name = carName;
} //constructor function

stop(){
document.write("<br/>"+this.name+" stopping...stopped.")
} //A method
}

//Creating Object
var toyota = new Car("Toyota Camry");
//Using Method Of Object
toyota.stop();
</script>

Copy PRACTICE IT

Why You Should Use Strict Mode Apart from avoiding failure of your programs in some browsers as a result of syntactical errors, one of the reasons to use the JavaScript strict mode as a beginner is to help enhance your grasping of JavaScript. That is, understanding to write good codes that can run on all JavaScript environment without issues arising from syntactical errors. How To Use Strict Mode To use strict mode in JavaScript, you make use of the string "use strict". This string should be place at the top of your program or script.
<script>
"use strict";
document.write("Strict Mode Environment Activated.");
</script>

Copy PRACTICE IT

Though strict mode was recently introduced and may not be supported in some browsers especially old browsers, using it will not produce any errors as the string will behave as though it is a string that was never assigned to a variable and as such, get lost while your program runs in the non - strict environment. Practices Not Allowed In Strict Mode
  • Using a variable to store whatever value without its declaration.
    "use strict";
    x = [1, 3, 5]; //Not Allowed
  • Deleting variables and functions.
    "use strict";
    var x = 25;
    delete x; //Not Allowed
Take a short quiz on JS Strict Mode.
Ask a question or tell us if JS Strict Mode is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode