There are those who will say terrible things about JavaScript. Many of these things are not true. When I was required to write something in JavaScript for the first time, I quickly came to despise it. It would accept almost anything I typed but interpret it in a way that was completely different from what I meant. This had a lot to do with the fact that I did not have a clue of what I was doing, of course, but there is a real issue here: JavaScript is ridiculously liberal in what it allows. The idea behind this design was that it would make programming in JavaScript easier for beginners. In actuality, it mostly makes finding problems in your programs easier because the system will point them out to you.
This flexibility also has its advantages, though. It leaves space for a lot of techniques that are impossible in more rigid languages, and having the knowledge of it different types of errors , makes it easier to overcome some of JavaScript’s shortcomings.
In this article, we'll discuss the basics- "Conditionals", "Loop" & "Function" which make JavaScript flexible to beginners.
CONDITIONALS: JavaScript as a language could be executed in various ways. One of the ways is conditionals which include IF statement, ELSE IF statement and SWITCH statement.
IF statement;
Example:
let num= 10;
if (num>0 & num!=0){
console.log( 'number is positive' )
} else{
console.log('number is negative')
}
output: number is positive
ELSE IF statement:
Example:
num=10;
if( num>=0, num!=0){
console.log('number is a natural number')
} else if(num<=0, num!=0){
console.log('number is a decimal number')
}else{
console.log('number is null')
}
output: number is a natural number.
Switch Statement:
Example:
let day='monday'
switch(day){
case'monday':
console.log("it's the beginning of a new week")
break;
case 'friday':
console.log("thank god it's friday")
break;
default:
console.log("it's a regular day")
}
output: it's the beginning of a new week
LOOPS: A loop is a control statement which allows us to repeatedly execute block of code.
Do While Loop: A do while loop is a control structure that executes at least once, and stops only after providing a termination in the while statement.
Example of a do while loop
let k=1;
do{
console.log("count:"+k);
k++;
} while(k<=5);
the above code execute for k count= 1, 2, 3, 4, 5. terminating at k count=5
For Loop: This executes all it statement in the parentheses after the for statement. The parentheses contains two semicolons the first semicolon initializes the loop while the second semicolon check whether the loop must continue the final part update the state of the loop after every iteration.
Example: a for loop with a breaking
for (let i=1; i<=10;i++){
console.log("count:" +i);
if (i===5){
console.log("breaking the loop at count 5");
break;
}
}
output: count: 1, 2, 3, 4, 5.
While Loop: A statement starting with the keyword WHILE create a loop, the word while is followed by an expression in parentheses and then a statement.
For example:
let j=1;
while(j<=10){
console.log("count:"+j);
if(j===3){
console.log("breaking the loop at count 3");
break;
}
j++
}
output
count:1, 2, 3.
FUNCTIONS: JavaScript can be executed as a function. A function is a set of code required to perform a task
Regular Function:
Example:
function calculateRectanglrArea(length, width){
return length*width
}
const rectanglerArea= calculateRectanglrArea(5,8);
console.log("rectangle area:", rectanglerArea);
output: rectangle area = 40
Function Declaration: JavaScript could be evaluated by declaration. this is the method of assigning value to a function.
Example:
console.log("the future says:", future());
function future (){
return "expect the unexpected";
}
output: the future says: expect the unexpected
Function Expression:
Example:
const isEven = function(number){
return number % 2 ===0;
}
const checkNumber = 7;
console.log(checkNumber + "is even:", isEven(checkNumber));
output: 7isEven - false
Scope: Each binding has a scope, each scope has a "look out" into the scope around it
For example:
const halve = function(n){
return n / 2;
};
let n = 10;
console.log(halve(100));
console.log(n)
output:
50
10
Hoisting : Hoisting is a method of which variable and function declaration are moved to the top of their scope before code execution. This implies that no matter where the function is declared, they are moved to the top of their scope before execution
for example:
function bakecake(){
function prepareBatter(){
console.log("mix flour, sugar, eggs, and milk.");
console.log(" pour the batter inside the cake pan.");
} prepareBatter();
console.log("bake in the oven.");
}
bakecake();
output:
mix flour, sugar, eggs, and milk.
pour the batter inside the cake pan.
bake in the oven.
Nestling : Nestling is when writing something inside of something, i.e when a function is inside another function
For example:
const hummus = function(factor){
const ingredient = function( amount, unit, name){
let ingredientAmount = amount * factor;
if(ingredientAmount > 1){
unit = 's';
}
console.log('${ingredientAmount} ${unit} ${name}');
};
ingredient(1, "can", "chickenpeas");
ingredient(0.25, "cup", "tahini");
ingredient(0.25, "cup", "lemon juice");
ingredient(1, "clove", "garlic");
ingredient(2, "tablespoon", "olive oil");
};
Arrow Function: The arrow function is executed in a situation where by the arrow comes after the list of parameters and is followed by the function body
For example:
Const greetUser = (name) =>{
return"hello," + name +"!";
}
const greeting = greetUser ("abdulmateen");
console.log(greeting);
output: hello,abdulmateen!
Reference:
Marjin Haverbeke: ELOQUENT JAVASCRIPT; 3RD EDITION(2018).