Javascript Accessors in action ๐Ÿš€

Javascript Accessors in action ๐Ÿš€

ยท

1 min read

While discussing with my friends I came across one interesting question which excites me to create this blog entry.

The question was, how the following conditions get satisfied to print the text in the console.

How I solved it? ๐Ÿ’ก

Let's solve this with the help of Javascript Object.

First, we will define an object and use one of the Object Accessors.

Basically, there are two types of accessors in javascript

  • getters

  • setters

Here we are using get also we can say getters.

More about the accessors can be referred from here, https://www.w3schools.com/js/js_object_accessors.asp

Now we can see here that the output is printing that log as we expected.

It satisfies the first condition with the temp's actual value for the very first time because get accessor is not called until the value of the temp is actually used. This is because JavaScript uses a process called lazy evaluation.

Please let me know if you have any other approach to solve this.

Happy coding ๐Ÿ˜Š

ย