Some Important JavaScript Basics You Need to Know 👐

Rahat Chowdhury
Geek Culture
Published in
8 min readMay 5, 2021
JS along with EcmaScript6

Numbers :

JavaScript Numbers are so fun to learn. 😃 Believe me, I am not kidding. If you fear JavaScript, it means , either you didn’t find a good source to learn or you just don’t wanna learn. 😝

However, JavaScript Numbers have done our mathematical calculation in coding very easy. For example, if you want to add 6 & 7 and see the result , you just type console.log(6 + 7) and here you go with the result 13 .

By the way, if you are wondering what is console.log, it’s a process to print out the result on the console . Again if you want to subtract 10 from 45 , you just type console.log(45-10) and you see the result 35 on the console.

Suppose you have to calculate a division operator (45 divided by 4) but answer only the remainder instead of quotient. So, you just type console.log(45 % 4) & boom! You get your result 1 There is a lot more fun things to do with numbers . Will write again about that another day

String :

String! What comes to your mind when you hear the word? It’s obvious, isn’t it? So, basically the word says its characteristics itself.

You can imagine a set of words or letters, or numbers or characters or anything in the world that goes with u (just kidding 😄) . However, if u want an example of string , I will give you a good example .

Let’s say ‘String’ The word itself is a string. Didn’t get it? 😉 Okay let me simplify . If I break down the word , I get ‘S’, ‘t’, ‘r’, ‘i’, ’n’, ‘g’ total 6 letters. So, I can say the word ‘String’ is a string of 6 letters. Now , whenever you want to make something string just conclude that in a quotation mark &your work is done. You have made a string.

Example : console.log('I am learning and implementing string') & you see the result yourself.

You can also convert a number into a string by just adding an empty string . For example : 4 + '' it gives us '4' as a string.

Boolean :

Boolean is what you call a true or false. As simple as that. If you want a condition in your code, then you remember Boolean. However there are so many falsy values but only a few truth values .

false, undefined, null, 0, empty strings ' ‘, NaN, these are the falsy values. Again , true itself is a truth value & 1 or any number larger than 1 is a truth value.

I am gonna give an example & you will get the idea . console.log(Boolean(false)) result false &

console.log(Boolean(123) result true

NaN :

It is the abbreviation of Not a Number. I mean, yeah I know you are not a Number but why do I need you❓. Basically while coding we might need some number testing, like , checking a value if it’s a number or anything else.

There we know the sibling of NaN i.e. isNaN(). By calling this method isNan() we can check whether the value is a number or not. The method returns a Boolean value (true of false) .

If the method returns true , it means, the value is not a number . So, you get the idea what if the method returns false. 👍

Operators :

+ * / these are some common operators we are familiar with, ain’t we? 😉 Well, JavaScript gives us some more operators to make our calculations easier.

+= -= ++ -- % these are some more operators which I am gonna explain simply with examples.(ignoring the console.log parts ).

2 + 3 results 5 , 2 — 3 results -1 , 2*3 results 6,

again, 6/2 results 3 ,

6%3 results 0( I know it’s a little bit confusing for the beginning but it’s easy) .

Actually 6 divided by 3 is 2 and there remains nothing else i.e. 0 & the % operator gets exactly the same value. I mean, first you divide, then after division you check the remainder. The % operator gives us the remainder

Now for the tricky part, 5 += 6 results 11 .How? Okay let me explain.

The += operator does the addition along with the assignment. a += b means a = a + b that’s the idea. Now you say what will be the result of 6 -= 4 . Try it. It exactly does the same thing as += but only subtract first & then assign the value to left operand. So, a -= b means a = a — b . The ++ operator does increment the value by 1 and decrements by 1, so 5++ means 6 and 5-- means 4 You can also add two or more strings with + operator.

Example :

'hi' + 'there' + 'my' + 'name' + 'is' + 'youKnowMyName' though this code will result this himynameisyouKnowMyName cause there is no gap or empty strings in those strings, you can modify the string like this

'hi ' + 'my ' + 'love' or 'hi' + ' ' + 'dear'

results hi my love & hi dear

Math :

It’s an object in JavaScript which has vast amount of math functionality in it. Suppose you need to calculate the area of a circle. What’s the formula? You don’t know?

Okay fine, let me tell you, it’s PI (which is 3.1416..) * (radius)². In JavaScript we write this formula like this Math.PI * (radius * radius) . However, you might have to calculate a number⁶ or number¹⁶⁵ , you can’t multiply like number * number * number so many times, right? So Math has a function named pow which takes two parameter as it’s base and the power respectively. So, we can write Math.pow(number, power)

for example : Math.pow(5 , 2) is 25 . There are so many things like this in Math that you can play with

Conditional / Control Structure :

These are some easy structures in JavaScript. I will directly go through example.

if(condition){ statement; }

else{ statement; }

This is the basic if else structure. Suppose you need to select one from two of conditions for example, you can’t decide which way you should go, left or right, that’s a two way condition , so we apply if else condition here.

Again if you encounter more than two ways conditions then you apply if → else if → else structure that looks like this

if(condition1){ statement; }

else if(condition2){ statement; }

else if(condition3){ statement; }

else{ statement; } .

There are some other control operators such as

=== > < >= <= == != !== these all are some interesting operators .

Let’s see some examples with them. if(a == b) this checks whether the two values are same or not (might both not be the same type)

whereas if(a === b) checks exactly the same condition but here type also is a factor.

Suppose a = '4' a string & b = 4 a number , we compare these two with == operator, if(a == b) this condition is true but on the other hand, the === operator, if(a === b)checks the value and return false cause a string type and a number type is not a match! (like me & my girlfriend 😔)

(jokes apart) To be double equal ==they don’t see their type, if the values are same, they match, but to be triple equal ===they have to be of same type

Thus, '33' == 33gives true & '33' === 33 false, simple as that.

Similarly '33' != 33 false & '33' !== 33 true

Functions :

To me, functions are the easiest to recognize. If you see something like this function anyFunName()

{ some statements ; } then you immediately know that anyFunName is a function cause it has the function keyword before it, pretty much obvious, isn’t it? Exactly.

Sometimes, a function declaration might be tricky to recognize. For example : let aFunThing = () =>

{ some statements ; } here a aFunThing is a function but it’s called arrow function, why? watch that arrow sign after the () ? that’s why! 👈

However, a function is so useful in JavaScript. Most of the coders do not code without function. In everyday life, they use functions. A lot more fun things can be done with functions. Details of function will be posted later in another blog (most probably) 🙏

Variables:

A variable is a value that varies from here to there. It’s the most simplified definition I can give. ✊ In coding , programmers sometimes need some changeable value which they call variable. But it has another plus point. A variable can also store a value , change that value & again store that changed value.

Variables in JavaScript are declared with let, const , var these keywords. let is used to declare a block level variable which means the variable can be accessed & changed only within the block but var acts like a global variable which means anyone can access this value from anywhere. const is the short form of constant which means not changeable. Some values are constant in mathematics (such as PI = 3.1416). In JavaScript coders use const to declare a value constant & that means the value can not be changed. Example :

let a = 5;

a = 15;

console.log(a) that gives 15, However, const a = 5; a = 10; will give you an error which tells you that you cannot assign a value to a constoperand. To explain the difference between let & var I need to use functions , so just play along with me, okay?

var a = 5; function timeTable(){a = 10;} a = 35here we have declared & initialized our variable a as var so the value changes & be 35after the last statement outside the function,

in this same example if we used let instead of var we would get the same result, but if we used let inside the function like this

function timeTable(){ let a = 10;} a = 100;then the value would not be changed, rather it would give an error, cause outside the function a is not recognized

Loop :

The name says itself what it does. Sometimes programmers need to do some changes repeatedly & loop is the process how they do it. There are different kinds of loops such as for while & do-while but again there are some variations of for loop such as forEach() for()Of for(In ) I will just give example with for loop.

Suppose you want to print out my name is Bill Gates 1000 times, what will you do ? write console.log() 1000 times? absolutely not! So, you use a simple for loop to repeat the console.log() 1000 times.

for(let i = 0; i < 1000; i++) {console.log(‘my name is Bill Gates')}

see? you just write one line and print 1000 lines ! magic! 😶

Arrays :

Array is another object material in JavaScript which looks like a simple array but works like an Object. Example : let myFriends = ['friend1', 'friend2', 'friend3', 'friend4']; console.log(myFriends); if you just copy & paste the code you will get a little bit idea of arrays. Array is recognized with [] ,remember that (ninja tricks 😛). To know the total item in an array just type arrayName.length

You can also loop through an array like

for(let i = 0; i < myFriends.length; i++) {console.log(myFriends[i];)} this will print every names in the array, try it out

--

--

Rahat Chowdhury
Geek Culture

Junior Front-End Developer trying to learn & broaden the knowledge