← Back to list

JavaScript Series Part-5 (Conditionals and Functions)

To use conditionals, we use if, else if and else keywords. Statement inside if gets executed if that is true. If that happens the further…

Shahmaruf Siraj Mugdho · 2025-09-09 16:44 · 1 claps · 3.8 min read
#function #conditional #javascript #coding #programming
Open on Medium ↗
Wiki topics: 💻 · Programming 🌐 · Web Development

JavaScript Series Part-5 (Conditionals and Functions)

To use conditionals, we use if, else if and else keywords. Statement inside if gets executed if that is true. If that happens the further conditions will not be checked. Similarly, statement inside the else if gets executed if that becomes true and others remains unchecked. But if your scenerio suggests to check both the if and else if conditions, then you should use if instead of else if.

let age = 34; 

if(age == 0){                  
    console.log("welcome"); 
}
else{
   console.log("Get Out");   // it will be executed
}

Make sure to use == operator to check the condition. If you use = operator (assignment operator), then the value will be checked to see if it’s truthy or falsy and the decision will be taken considering that.

But here comes a question, can we give conditions as we like or should there be any order.

Let’s solve the FizzBuzz Problem.

But, There is an error found in the output. For 15, it should have printed FizzBuzz but it printed Fizz because we checked the condition for 3 at first and the later part is being ignored in that code. So, what we should do is to check that particular condition at first or move that condition block at first. Then this problem is resolved😊

Let’s Solve another problem. Suppose we need to print which day is today using Date Object. In JavaScript, the day starts with Sunday. So, Sunday is 0 and the upcoming days are incremented by 1. But you can see that we need to check the similar conditions manually. This is less readable code. In this case, we can use Switch which will make it more readable and clean.

Using Switch:

  • case = entry point
  • break = exit point
  • Without break, execution “falls through” to the next case(s)

If you do not use break, then it will print the remaining even it is not true. So, case is the entry point of your output and break is the exit point where you want it to stop. So, do not forget to use breaks when you use switch cases. You can add another case named default, if neither of the conditions is true, then it will be executed.

Let’s see another example with if-else. If age and location match, then execute that block.

**...`` (backticks)** → template literal

**${...}** → insert variable or expression directly inside the string

In JavaScript, strings can be wrapped in:

  • single quotes: 'Hello'
  • double quotes: "Hello"
  • backticks: Hello

👉 Backticks are special — they allow:

  • String interpolation (inserting variables directly inside a string)
  • Multi-line strings (without needing \n)
let x = 10, y = 20;
console.log(`Sum is ${x + y}`);  // Sum is 30

Let’s discuss about function. function sayhello(){}; this is the definition of this function. It doesn’t get executed immediately. The interpreter only reads this part, but executes it only when this function is called or invoked. Here, this function is being executed or printed when the interpreter gets to the sayhello() line.

sayhello() will print “Hello World” every time you invoke it or call it. But what if you want to make it dynamic and print different things..for example: Mars, Moon, Jupiter and so on. How to do that?

You will need to define parameter ar arguments. You can take more than one parameter as well. But the number of parameters and arguments has to be the same. You can use default parameter also, it will work when there is no arguments provided.

You can assign a function to any variable in JavaScript and that variable will save the reference of that function in the memory address. But this time the function identifier or function name should be removed as there can’t be 2 identifier at the same time. This type of function is called anonymous function.

let greet = function(){                    // anonymous function
console.log("Hello world");
}
greet();

Here greet = reference

greet() = invoke

Hit the clap 🙌button if you have enjoyed reading this blog.


메타데이터
post_id
caecee8d1507
slug
javascript-series-part-5-conditionals-and-functions-caecee8d1507
url
https://medium.com/@shahmarufsirajmugdho/javascript-series-part-5-conditionals-and-functions-caecee8d1507
canonical_url
https://medium.com/@shahmarufsirajmugdho/javascript-series-part-5-conditionals-and-functions-caecee8d1507
author_url
https://medium.com/@shahmarufsirajmugdho
status
ok
fetched_at
2026-07-09 04:10:03