Conditionals
Conditional statements allow me to execute codes based on the conditions given. These conditions are boolean expressions, that is they return either true or false. E.g.
There are several types of conditional statements in Javascript, but the most common are:
if... else:
This conditional statement executes just one block of code if the condition being passed is true, and another if the condition is false. E.g.
if...else if.....else:
This statement checks for multiple conditions. the code will execute the first block of code that has a true condition. E.g.
Switch:
This is used to compare a variable to several different values. The code will execute the block of code that responds to the value that matches the variable being in comparison. E.g.
LOOPS
Loops are used to execute a block of code repeatedly until a certain condition is met. This automates repetitive tasks and iterates over collections of data.
The main types of loops are:
For Loop:
This loop is used to execute a block of code with a specified number of times. The loop iterates over a range of values, and the current value is available in the loop as a variable. E.g.
While Loop:
This allows the code to run repeatedly as long as a condition is true. Here the condition is evaluated before each iteration before each iteration of the loop. E.g.
do...while loop:
The loop executes a block of code at least once, then continues to execute the loop as long as the condition is true. The condition is checked after each iteration of the loop. E.g.
FUNCTIONS
Function Declaration
This is just the formal way of defining a function. This has the function's name, parameters, and the code that makes up the function's body. A declared function are usually being reused in a code by calling the functions name.
In declaring a function, we can pass a parameter into it and the incoporate it while calling the function.E.g.
Also, we can pass in more than one parameter into it and execute any command given.E.g.
Function Scope
The function scope is dealing with the variables that are declared using the function. A declared variable inside the function is called local variable and are accessible anywhere inside the function. A global variable can be used anywhere in the code and not only restricted to inside the function.
This is why we must be very careful in declaring a global variable ☝️☝️☝️
Nesting Functions
When we use the word Nesting, it means putting a particular thing in another thing. A nested function means declaring a function inside another function and this helps to make codes more organizable and also helps to provide privacy in codes. E.g.
Conclusion
Functions in JavaScript act like specialized tools that perform specific tasks when we call them. They help us organize and reuse code, making our programs easier to understand and maintain. We can create our functions to do specific tasks or use ready-made ones that come with JavaScript.
Reference
You can follow me on Twitter to stay updated on web development content and cool projects. Let's code and share ideas together.