Overview of Conditionals, Functions, and Scope
Continue Coding
Storing information in variables and printing out information gives us a good basis for starting our programming journey. The next step is to use conditionals — to build out programs that account for different scenarios. We’ll also make use of functions to reuse code and execute blocks of code at once. Afterward, we’ll use scope to keep track of when and where variables and values are accessible. Let’s explore these concepts in more detail.
Adding Flexibility with Conditionals
Every day we make decisions using the best information we have. Even simple decisions like, getting up at a certain time, or sitting down to eat a meal, are all guided by information.
This decision-making ability gives us the flexibility to address even more complex situations. This same idea of decision making to address different situations can be incorporated into our programs through conditionals. Here’s an example:
if(correctPassword) {console.log('You are now logged in.');} else {console.log('Incorrect password.');}
Above, we’re writing code to check if a user’s password is correct — if it is, they can log into their account. And we can also account for when the password is wrong, so they can’t log in.
Using conditionals, our program becomes more robust and flexible!
Reusing Code with Functions
Sometimes when we do a task, we do it more or less the same way. Let’s say we’re folding clothes, specifically a shirt. We need to take the shirt, lay it down, make the necessary folds, in the right place, in the right order, and repeat for the next shirt. We do it so often, that we know exactly what to do every single time we need to fold a shirt.
function foldShirt() {console.log('Shirt is folded');}foldShirt(); // Prints: Shirt is foldedfoldShirt(); // Prints: Shirt is foldedfoldShirt(); // Prints: Shirt is folded
As seen above, this same idea of repeating tasks also exists in programming. To spare ourselves from writing the same lines of code over and over again, we can bundle the code into a function which is one block of code that has a given name. The function, foldShirt()
saved us time and effort by not having to write the same console.log()
statement each time. Now when we want to repeat our collection of tasks, we simply call the function.
We can even allow functions to take in user input. If we modified our foldShirt()
function into a more general foldClothes()
function:
function foldClothes(clothing) {console.log(`${clothing} is folded`);}foldClothes('Pants'); // Prints: Pants is foldedfoldClothes('Sweater'); // Prints: Sweater is folded
Now our function does more than just fold shirts, it can fold any type of clothing! This ability to include user input is because of parameters and arguments. We’ll cover this in even greater detail later on.
Within Scope
As we work on creating more code and blocks of code, it’s important to know when values and variables can be accessed. This ability to access specific values and variables is known as scope.
When used in combination with conditions and functions, the concept of scope tells us how to organize our code to make sure our program has all the right information to work properly. Keep this in the back of your mind as you work through the material.
Accumulating Knowledge
With these three new concepts, your ability to write advanced programs will increase dramatically. But remember, you don’t need to build anything from scratch — with Wix and Velo, you can develop these advanced programs even quicker!
Author
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
Methods and Functions
Use this article as a reference sheet for JavaScript methods and functions. - Article
Setting Up a Backend
The backend is an important part of any website. It's where long-term data is stored, and where code is executed to handle interactions between your website and it's visitors. Wix has conveniently added the ability to control certain aspects of the backend with web modules.