More Interesting Facts in JavaScript

In the previous post, we saw some basic interesting facts in JavaScript. In the same way, we are going to view other interesting facts in this post. If you haven’t viewed that post, then I suggest you do have a loot at it.

Javascript Interesting Facts. We are going to see more interesting facts in this post. There is JS written in the photo.

More Interesting Facts

Var and Let

Let’s see the code below:

if (false) {
    var newVar = 'Nepcodex';
    console.log("Hello World");
}
console.log(newVar);

In the above code, Hello World is never printed because the logic is false for the block. However, a new variable named newVar is declared but undefined. Therefore, if you don’t want to have this behaviour of var, you can always use let.

Function Declaration vs Function Expression

We can define javascript functions in basically two ways, function declaration and function expression respectively as follows:

function hello() {
    return "Say Hi from NepCodex";
}
let hello = function() {
    return "Say Hi from NepCodex";
}

To know the difference between these two syntaxes, let’s call the functions above the declaration. You will see that, second gives you the error and you know the reason why.

hello2();   // "Hello2"
function hello2() {
    return "Hello2";
}
hello3();  // cannot access 'hello3' before initialization
let hello3 = function() {
    return "Hello3";
}

Numbers

Some interesting facts in javascript contains the following things too.

alert(9999999999999999); //16 nines... 10000000000000000
let a = -0; // -0
let b = 0;  // 0

Array Length Property

The length property of array doesn’t give the count of the elements in the array. Instead, it gives the greatest numeric index plus one. Moreover, it is writable too, i.e. it truncates to the desired length.

let place = [];
place[6] = "Kathmandu";
console.log(place.length); // 7
console.log(place); // [empty x 6, "Kathmandu"]
place.length = 2;
console.log(place); // [empty x 2]

Array Sort Method

The thing important to realize is that, sort() functions sorts the elements of array as strings. For instance, let’s look into the output of the following code:

let arr = [1, 17, 2, -2, -25, 25, 3]
arr.sort()

However, we can sort using our own function which the sort takes as a callback.

let arr = [1, 17, 2, -2, -25, 25, 3]
arr.sort(sortNumberAscending);
function sortNumberAscending(a, b) {
    return Math.sign(a - b);
}

arguments Keyword

We have heard about the command-line arguments in other programming languages. But how does JavaScript implement arguments? Let’s create a function that concatenates the elements of an array.

function concat() {
    return Array.from(arguments).reduce(addStrings, '');
}
function addStrings(total, currentItem) {
    return total + String(currentItem);
}
let address = ["Nepcodex", " ", "Kathmandu", ", ", "Nepal"];
concat(...address); // Nepcodex Kathmandu, Nepal
address = ["Facebook", " ", "USA"];
concat(...address); // Facebook USA

Spread and Rest Operator

You can look about Spread and Rest Operator in the following post here: https://nepcodex.com/2019/08/spread-and-rest-operator-in-javascript/

Conclusion

To sum up this post, we can say that programming in JavaScript is fun. However, if we do not give proper attention in the syntaxes, then we might get some anomaly behaviour than we expected.

Furthermore, if you want to learn about django rest api, please follow the link: https://nepcodex.com/2019/07/django-rest-api-from-scratch-part-1/

In the same way, you can follow https://gist.github.com/kriss-u for the pieces of codes.

0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to top

Send help to Morocco.

X