JavaScript Basics
Conditionals
Loops (for, do .... while, while)
Functions
JavaScript is one of the most understandable languages in coding. Therefore, understanding it's basics is a game changer in ones journey to crafting whatever amazing software is in mind.
What are Conditionals? These are structures that help in executing codes based on conditions. For example, if this is so, the code will return true, if the conditions are not met, the code will return false.
The main item used for executing conditionals is If.
If (condition) {
// Code to be executed if the condition is true
}
This can be used in building finance websites where a certain condition needs to be met before a customer can access what they need. For example, if one programmes an ATM, the password has to be correct (the condition is the password) and if it's not met, it will return false
Conditional has another variable. In a case where there are two choices to be made, if... Else can be employed for certain situations.
If the first condition is not met, the code automatically goes for the next which will then return true
As exemplified by the tutor, if the bank app requires a 1k Commission from a receiver who is from another bank and will be receiving 20000 and above, the conditional will be given details that will either prove true for this or run the next code which is for the customer that is within the bank and will be charged 500 naira
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
From my understanding, this can be used in multiple options, not just for two. If the first code runs false, the next also does the same, there's space for more depending on the requirements of the software being developed. The use of If... Else helps in carrying out this conditional.
The next Basic requirement of understanding JavaScript is Loops.
Collating data can be stressful but with JavaScript, we have loops to help in that. An example used during class was if one needed only the even numbers from 0 to 100. This will run till all the numbers are complete but be careful, if it's not done right, it'll run till the system shuts down. Scary. They are control structures that help in executing codes repeatedly. It can do it as many times as required.
We'll discuss the variable For first. This is used for a specific range of values like the items in an array or numbers like used I before.
for (; condition; increment/decrement) {
// code to be executed in each iteration
}
The condition inclusion helps it stay in focus and does exactly what you need it to do.
Another structure of the Loop in JavaScript is Do ... While. This function is used to execute the code at once before conditions are put into consideration. It will run the code once, then check for the condition, it has been met, it'll stop running but if not, it'll keep running till the condition is true. The do...while loop in JavaScript is used when you want to execute a block of code at least once, and then checks the condition to see if it's true. It runs a block of code, and then it checks if a condition is true. If it's true, it repeats. If not, it stops.
let count = 0;
do {
console.log("This will run at least once");
count++;
} while (count < 3);
In this example, the code inside the loop will run at least once because the condition (count < 3) is true initially. It will continue to run until the condition becomes false.
The 3rd Function of Loop in JavaScript is While. This specifies that when the condition is met, the loop keeps running. If the 1 plus 1 equals 2, the loop will run.
while (condition) {
// code to be executed as long as the condition is true
}
This is a bit similar to do ... While. The difference is that this won't run first, it's simple going to only work if the condition is met but do while runs first then takes action afterwards.
The Third JavaScript knowledge that's a pillar in our journey is the Functions. What are functions in JavaScript? Functions in JavaScript are like your own little soldiers. They help you prevent repetitively doing one thing over and over again. Once you set them up, you can just recall it and it'll perform the required action. They are specific codes that mean specific things which makes coding easier.
These include Reusability, parameters, Encapsulation and others. With these tool box, you can get work done faster.
Here's an example of the Parameters Function
function multiply(a, b) {
return a * b;
}
let product = multiply(5, 3); // Arguments: 5 and 3
console.log(product); // Output: 15