Java Script

Deshani Warnakulasuriya
2 min readMar 21, 2021

Chapter 2

As I mentioned in chapter 1 today I am going to talk about ‘this’ keyword and strict notation.

First I will talk about ‘this’ keyword in java script.

‘this’ keyword

‘In java script ‘this’ keyword is used to refer the object that it belongs to. According to the place the ‘this’ keyword is going to use value of ‘this’ keyword will be different.

In a method ‘this’ refers to owner of the method. To demonstrate this let’s take employee object.

when ‘this’ keyword using in a function it will refers to global scope so ‘this’ will refer the window object. let’s see below example.

output of above example

So in above example, ‘this’ keyword in This function refers to window object. Therefore, this.myNum will return 200 . If you didn’t mention this then it will refer the local myNum variable defined in refThis() function.

If you use strict notation then value of ‘this’ will be undefined in the global scope.

‘this’ used in inner function also refers the global window object.

When creating an object of a function then ‘this’ keyword will point to that particular object. Below example demonstrate the above scenario.

output of above exmple

Strict Notation

If we use ‘use strict’ before writing our code in java script, then the code will be executing in strict mode. This ‘use strict’ is not a statement it is a literal expression. In strict mode you cannot have undeclared variables. This strict notation help programmers to write clean codes.

defining strict notation

--

--

Deshani Warnakulasuriya

I am a Software Engineer who loves to learn and loves to share what I know.