10 Basic JavaScript Things You should Learn Today

Farhan Absar Tafader
4 min readMay 5, 2021

Data Types VS Primitives

Data type is what type of data do JavaScript deals with. JavaScript always works with Strings, Numbers, Boolean, Symbols (New Addition in ES6), Objects (Function, Array, Date, RegEx), Null, Undefined.

And primitives are Strings, Numbers, Boolean, BigInt, Symbol, Null, Undefined. Now the question is if primitives are included in data types, then why they are particularly called as primitives? The answer is primitives are not object and it has no built-in method to use. These are just data, so they are separated from the Objects.

a < b returning “true”; why??

Have you ever seen that a var is assigned with a and another var is assigned with b, now if you are try to compare them it is returning a < b as a result. Little tricky right? The question is how this happens? Do “a” and “b” has some values? The answer is neither positive, neither negative. There is a reason. Every JavaScript compiler engine (V8, SpiderMonkey, Chakra) takes the characters as a binary value. So, “a” is less coded then “b” that’s why JavaScript keeps “b” bigger value. This is the matter of following so-called “dictionary” or “lexicographical” method.

let str1 = “a”;

let str2 = “b”;

console.log (a < b); // true

Again, how about “A” and “a”? ok here “a” < “A” return true. Because of the same reason it follows which means how they are indexed in UNICODE.

Do strings have character code in JavaScript?

The modern JavaScript has. If any of your string you want to know what is the character code the you have to apply a simple method charCodeAt(); charCodeAt() takes an input in which position do you want to see the character code, and returns a numeric value.

let word = “WORD”;

console.log(word.charCodeAt(“W”, 0)); // W 87

“0.1 + 0.2 = 0. 30000000000000004”, Do You Think This is Wired?

Well in terms of sum method, sometimes we face these types of values which blows our mind.console.log (0.1+0.2) // 30000000000000004

This is not actually a problem, but this is some built-in functionality which JavaScript runs in the browser.

JavaScript follows the UNICODE number method which derived from C or JAVA. Here it calculates every single bite from each number. So (2^56 - 1) in this integer range, it will return how many bites it calculates.

When we use Math.random() * 100 it also returns 101 where the range is 0 – 99.

BigInt: new miracle in JavaScript?

After ECMA2015 or ES6 release, BigInt is one of the new ideas to the developers. How it works?

JavaScript works with binary, decimal, octal, hexa-decimal number formats. But calculating a large hexa-decimal was a tough thing.  Suppose you have a number “9007199254740991”  how will you convert it into hexa-decimal?  Here comes the play of BigInt.const largeHex = BigInt("0x1fffffffffffff")console.log(largeHex) // 9007199254740991n

Confused About Split, Splice and Slice?

These three are common array methods in JavaScript. Interestingly, these methods split an array so what is the difference? Yes, there are little difference.Slice method takes two parameters, one is init, means the initial value, and another is the end, means where it will stop splitting. It will return an array. But it will not change the parent array.
let players = [“Messi”, “Ronaldo”, “Iniesta”, “Xavi”, “Rooney”];
players.slice(0, 2); // [“Messi”, “Ronaldo”]so it means it will stop and return the very previous value of the end point. It stopped at “Iniesta” and returned till “Ronaldo”.In splice method, it will do exactly the same as slice but it will change the parent array.Split is little bit different from them. It will work when the array turned into a string.let playerStr = players.toString();playerStr.split() // ["Messi,Ronaldo,Iniesta,Xavi,Rooney"]now what command you give in the split input it will separate the whole string.For example, if now we input a “,” then the magicpalyerStr.split(“,”) // ["Messi", "Ronaldo", "Iniesta", "Xavi", "Rooney"]

for loop iterate with “++” get rid off?

Here we can use a very short hand method to iterate an array with for loop.let players = ["Messi", "Ronaldo", "Iniesta", "Xavi", "Rooney"];now we want each player.for (let eachPlayer of players) {console.log(eachPlayer) // Messi Ronaldo Iniesta Xavi Rooney}

Have fun With “includes”

So basically build your search  filter with includes!Includes mainly an array method which searches and returns Boolean about the item in the array.let players = ["Messi", "Ronaldo", "Iniesta", "Xavi", "Rooney"];console.log(players.includes(“Iniesta”)) // true;

Some Is Claiming Some Function?

Didn’t no about some() method? Ok he is super cool because he takes function in his input parameter.some() also an array method. It works as the includes() but it takes a function as a parameter.Suppose you are in a job interview where the CEO is filtering if the developer is React expertlet   devs = [“Angular”, “Vue”, “React”];console.log(devs.some(dev => dev === “React”)) // true;

Every one is not React Dev? Call of The Interview Man!

every() is another array method which works just like some() but here it searches for every item is same.let   devs = [“Angular”, “Vue”, “React”];console.log(devs.some(dev => dev === “React”)) // false;

--

--