Sitemap
CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Modern JavaScript

5 min readJul 5, 2022

--

Let’s say that a variable is hidden outside of its defined boundaries. The k variable’s scope, in this case, is limited to the for a loop. Accessing variable k results in the error shown below.

Press enter or click to view image in full size

If we use var in this case, it will print correctly without any errors, however, the scope of var makes it difficult to see errors in long pieces of code. Let ensures that an effect will occur inside of that {} code.

If const is used in place of var, the identical problem will occur if the variable is declared inside a for loop. When using const outside of a loop, we must initialize the const variable there in the declaration row in order to avoid errors.

Because const is initialized twice, once in line 1 and again in line 3, the code following produces an error.

Press enter or click to view image in full size

Since scope affects the whole code from lines 1 through 8, using allow outside of the loop won’t result in an error.

Press enter or click to view image in full size

Const only provides protection for variables that aren’t objects or arrays.

As you can see below, after a const variable has been assigned, we are unable to give it a new value.

Press enter or click to view image in full size

But we can use an array that is constant and add 2 or more values.

Press enter or click to view image in full size
Press enter or click to view image in full size

It doesn’t matter if it’s an object or an array; it can add to or modify the value of the constant variable.

Arrow function in JavaScript

Using an arrow, we can make a function simpler. If we are passing two arguments, then brackets are required; if we are just passing one parameter, then there is no need for brackets.

Press enter or click to view image in full size

This keyword inside a regular function represents the calling function if one is written. However, if we are using an arrow function, this term does not indicate the caller.

Press enter or click to view image in full size

Hint -: this doesn’t represent the caller.

Object handling

If we are unsure about the key at the time of execution, we may use a placeholder as the key thanks to dynamic properties.

Key is sometimes quite lively. When we transmit an item through service, we are unsure of what the key would be. Any content that is provided by the service can be assigned to the placeholder as shown below.

Press enter or click to view image in full size

As you can see, line 1 already has a value, hence SQRT2 doesn’t require a value. When the dynamic property is enabled, an error message appears.

Get Harshana Samarasinghe’s stories in your inbox

Join Medium for free to get updates from this writer.

If we are unsure of the value at the time of execution, we can use a placeholder as a key, as seen below.

Press enter or click to view image in full size

Freezing objects

We may easily reassign a value to an object as seen below.

Press enter or click to view image in full size

However, if we use the freeze method, the value of the second object does not change since, after freezing, the object retains its original value.

Press enter or click to view image in full size

The item will get frozen as a result, rendering it unchangeable.

Use the freeze method if you want an object that moves from service to service or function to function but you don’t want to modify its value.

This freeze only affects first-level values; it has no effect on objects at inner levels.

Press enter or click to view image in full size

Name is the first-level variable, while t1 and t2 are second-level variables, as you can see. Only first-level variables, such as names, are subject to freezing. Thus, the name has not been altered. T1 alters since the freeze approach has no impact there.

Template

Press enter or click to view image in full size

Override functions

Press enter or click to view image in full size

When the function is called through an object, we may override it with anything. Classes may have functions, but at runtime, we may choose to override them in overriding functions, for example.

Destruct

Press enter or click to view image in full size

Instead of demonstrating here, we could just write PI and SQRT2 the same way we learned to do it in math class.

Press enter or click to view image in full size

When calculating area, we simply need a basis; no additional variables are required. We pass the object while supplying parameters, yet it calculates the area without any issues.

As you can see, when this is executed, two text files are created. Here, we have the option to disregard fs and just write it when using destruct.

Press enter or click to view image in full size

Promise

Http GET occasionally connects to the server and requests a response, the code must be wrapped in a promise as seen below. This will trigger the promise when it is completed if it is wrapped in a promise.

Press enter or click to view image in full size

This will result in undefined if the below function is used in place of the catch finally block. This is included within a promise object, but we first declare the length. Typically, we don’t wait for the outcome in this situation. This does not await the fulfillment of the promise.

This will return a promise object and wait until the promise is resolved by using the fetch web page. Once the promise was fulfilled, it moved on to the next one. If the await keyword is removed, it will become undefined.

The term “await” is only valid inside an async function. Removing the async keyword makes await ineffectively.

--

--

Harshana Samarasinghe
Harshana Samarasinghe

Written by Harshana Samarasinghe

“Truth can only be found in one place: the code.” - Associate Engineer — Java Technology at Virtusa Sri Lanka -

CodeX
CodeX

Published in CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers