10 interesting concepts on javascript….

Sagar Biswas
3 min readMay 5, 2021

As a front-end developer while we are familiar with javascript but sometimes it confuses us with its vast amount of topics. Here I will try to discuss 10 interesting facts.

  1. Forming an array from a string.
    It is quite an interesting and fun fact that we can make an array from a string.

just consider the following example

const name=”Javascript”
console.log(Array.from(name))

expected output [‘J’, ‘a’, ‘v’, ‘a’,’s’, ‘c’, ‘r’, ‘i’,’p’, ‘t’]

2. Want to verify all the values in an array? “every()” is there to help you. It returns a boolean value based on condition.

For example, you want to verify, it is ensured the age of a certain group is below 40. that means all the data presented in the array is passed against a certain condition, see the following example :

const users = [
{ name: “a”, age: 18 },
{ name: “b”, age: 20 },
{ name: “c”, age: 25 },
];
function checkAge(user) {
return user.age < 40;
}

console.log(users.every(checkAge));
output : true

3. Making star rating just from a number.
Suppose a user just gave a number as a rating. How can you convert this number as a star rating? Notice the following example.

const rating = 5;

var ratingArray = Array.from(Array(rating)).map(() => “*”);
console.log(ratingArray);

the expected result is [ ‘*’, ‘*’, ‘*’, ‘*’, ‘*’ ]

4. While adding a string to a number (or other value), this results in the conversation of everything into a string. This might shack you up a bit.

console.log(“5” + 77 + 99)
output: 57799

but,
console.log(5 + 77 + “99”);
output : 8299

5. Did you know, you can access the parameter of a function, unlike the traditional way we do?

function averageIncome() {
console.log(arguments);
let average = 0;
let sum = 0;
for (let i = 0, j = arguments.length; i < j; i++) {
sum += arguments[i];
console.log(sum, typeof sum);
}

average = sum / arguments.length;
return average;
}

console.log(“the average is”, averageIncome(2, 4, 6, 4));
output: 4

It’s not like that we need to keep this in our minds. this is not the best way to calculate average. The interesting fact is that parameters passed in a function can also be accessed by “arguments”. And it’s good to have a look at some interesting facts like this.

6. A function can be called anonymously. for example:

(function () {
console.log(“hello user”);
})();

Actually the following defines a function expression:
(function () {
console.log(“hello user”);
})

The last pair of “()” allows calling the function. In this parentheses “()” you can pass parameters.

const num = 6;
(function () {

console.log(num)
console.log(“hello user”);
})(num)

This function is as same as
function greeting() {
console.log(num);
console.log(“hello user”);
}

greeting(num);

7. Queue data structure using the array-

A queue is based on the (FIFO) principle, which means the first in first out principle.
So just think about the methods you follow to insert and delete items in an array.
Although there are several ways, push() and shift() are the main combination here.

  • Push method adds an element at the end of the array which is as same as enqueue operation.
  • Shift method removes the first item from an array. that means it’s a dequeue operation of which item entered first.

8. Stack data structure using an array

This data structure is based on the LIFO principle which means last in first out. Two operations are the main combination push and pop. Here elements are stacked on top of each other.

  • As LIFO sounds push() stores an item at the position of an array, and pop() method removes the element at the end of the array.

9. Although the String contractor sounds like it will return a string, the interesting fact is the if you see the type of it, the result will be an object. Consider the following example,
let string1 = “5 + 5”
let string2 = new String(“5 + 5”); // creates a String object

if we use the eval() method, the result is quite interesting

console.log(eval(string1))
output: 10
console.log(eval(string2));
output:[String: ‘5 + 5’]

10. you can use the conditional (ternary) operator that takes three operands and makes your life easy.

const age = 18;
const voter = age >= 18 ? “Yes, you can vote” : “You can not give vote”;
console.log(voter);

--

--