Blog # 5 JavaScript (3 important Concepts)

I have used JavaScript in the past, along with HTML and CSS, but was surprised to see the use of newer features I had seen in other languages like Java and C#.net being used heavily by JavaScript extensions like Vue.js. Some may consider these features to just be “syntactical sugar”, but once used, they tend to be really important in their ability to make your code more readable and useful.

I watch 2 YouTube videos on “The 5 Must-Use JavaScript” features, one by James Quick and one by PedroTech. This is my opinion of information I have consolidated from these tutorials.

1. Equality:

I have been used to seeing the “double equals” ‘—’ being used to differentiate between assignment statements (with a single =) and equality statements (with a double ==). This is pretty standard, but the use of a triple equals is new to me. As an example, if S1 is a string with the numbers “456”, then

(a). S2 = “456” assigns the value of S2 to “456”

(b). S2 == S1 compares “456” with “456” and returns TRUE

(c). S2 === 456 Compares String”456” with number 456 and returns FALSE.

This shows that (c) guarantees you are using the save values and object types (or primitives). (b) is useful because it works comparing both objects and primitive data types. Since the double equals seems to have been added to languages to differentiate between assignments and comparisons, the triple equals give a stricter control by guaranteeing value AND data type are identical.

2. Asynchronous JavaScript:

JS has been using callbacks since its inception. Over time it evolved into using Promises and Async Awaits.

GOOD – Callbacks: It’s older method where function pointers were passed to the callback function (like in C).

BETTER – Promises: This method uses error handling mimicking the try/catch/finally blocks used by most modern non-interpreted languages. E.g.

Set fetchData = new Promise();

fetchData.then(),catch().finally();

The logic for try goes in the then() method. If an exception occurs, the logic in the catch() block is called. In all cases, the finally() block is called for cleanup tasks.

BEST – Async Await:

This process is seemingly used more frequently, because it allows the developer to embed the logic for both the calling code and the receiving code to be implemented in the same method and is somewhat easier to use as a result.

3. Array Iteration:

Older method for iteration:

Let numbers = [1,2,3]

For (let i= 0; i < numbers.length; i++ {

 Console.log(numbers[i]);

}

Newer method:

Let numbers = [1,2,3]

Numbers.ForEach((element) => { console.log(element); }

This may seem like a minor difference, but, when understood properly, syntactical sugar like this can make your coding life simpler and less error prone.

Leave a Comment