Welcome to your JavaScript Introduction Lab. In this lab, you will teach yourself the foundations of JavaScript by reading short explanations, completing guided practice, and building small pieces of code on your own.
This lab is designed to help you build confidence with:
- variables
- conditionals
- functions
- loops
By the end of this lab, you should be able to read and write simple JavaScript programs using these core concepts.
By completing this lab, you will be able to:
- Declare and use variables in JavaScript
- Write conditional statements using
if,else if, andelse - Create and call functions
- Use loops to repeat actions
- Combine variables, conditionals, functions, and loops to solve simple problems
- Open VS Code
- Create a folder called
javascript-intro-lab - Inside the folder, create a file named
lab.js - Open the terminal in VS Code
- Run your code with:
bash: node lab.js
You may also complete some parts of this lab in the browser console:
- Open Chrome
- Right-click the page
- Select Inspect
- Click the Console tab
Make sure you understand this important idea:
JavaScript reads your code from top to bottom. That means the order of your code matters.
A variable is a named container that stores information.
You can think of it like a labeled box:
- the label is the variable name
- the value is what is stored inside the box
let name = "Jordan";
let age = 28;
let isStudent = true;In the examples above:
namestores textagestores a numberisStudentstores a true/false value
letis used when a variable value can changeconstis used when a variable value should not change
let city = "Philadelphia";
city = "Wilmington";
const school = "Code Differently";In your lab.js file, create variables for the following:
- your name
- your age
- your favorite color
- whether you like coding
Then print them using console.log().
Answer these questions in comments in your code:
- What is a variable?
- What is the difference between
letandconst? - What does
console.log()do?
A conditional allows your program to make decisions.
JavaScript uses:
ifelse ifelse
This means:
- if the condition is true, run the first block
- otherwise, run the second block
You will often use these in conditionals:
>greater than<less than>=greater than or equal to<=less than or equal to===equal to!==not equal to
let score = 85;
if (score >= 90) {
console.log("A");
} else if (score >= 80) {
console.log("B");
} else {
console.log("Keep practicing.");
}Create a variable called temperature.
Write a conditional that prints:
"It is cold."if the temperature is below 60"It is warm."if the temperature is 60 or higher
Create a variable called password.
Write a conditional that prints:
"Access granted"if the password is"javascript123""Access denied"otherwise
Create a variable called hour.
Write a conditional that prints:
"Good morning"if the hour is less than 12"Good afternoon"if the hour is between 12 and 17"Good evening"otherwise
Answer these questions in comments:
- What is the purpose of an
ifstatement? - When would you use
else if? - What does
===check for?
A function is a reusable block of code that performs a task.
Instead of writing the same code over and over, you can place it inside a function and call it when needed.
function greet() {
console.log("Hello, welcome to JavaScript.");
}
greet();A parameter is a value the function receives.
function greetUser(name) {
console.log("Hello, " + name);
}
greetUser("Taylor");
greetUser("Chris");A function can also return a value.
function addNumbers(num1, num2) {
return num1 + num2;
}
let result = addNumbers(5, 3);
console.log(result);Create a function called sayHello that prints a greeting.
Create a function called favoriteFood that accepts one parameter and prints:
"My favorite food is ____."Create a function called multiply that accepts two numbers and returns the product.
Create a function called isEven that accepts one number and returns whether the number is even.
Answer these questions in comments:
- Why are functions useful?
- What is a parameter?
- What is the difference between
console.log()andreturn?
A loop repeats code multiple times.
Two common loops:
forloopwhileloop
A for loop is useful when you know how many times you want to repeat something.
for (let i = 1; i <= 5; i++) {
console.log(i);
}This prints:
1
2
3
4
5A while loop continues as long as a condition is true.
let count = 1;
while (count <= 5) {
console.log(count);
count++;
}Write a for loop that prints numbers 1 through 10.
Write a for loop that prints only even numbers from 2 to 20.
Write a while loop that prints numbers 5 down to 1.
Write a loop that prints:
"Practice makes progress"five times.
Answer these questions in comments:
- What is a loop used for?
- When might you use a
forloop? - What could happen if a loop condition never becomes false?
Now it is time to put all four concepts together.
Create a function that:
- accepts one number
- uses a conditional to check whether it is positive, negative, or zero
- prints the result
Create a function that:
- accepts one number
- uses a loop to count down from that number to 1
- prints each number
Create:
- a variable called
studentName - a variable called
score
Then create a function that checks the score and prints:
"Excellent work"for 90 and above"Good job"for 75 to 89"Keep practicing"below 75
Create an array of 5 names if you already know arrays. If you do not know arrays yet, just create 5 separate variables.
Then:
- write a loop
- print each student name one at a time
Bonus: If you have not learned arrays yet, skip this one or ask your instructor for support.
Complete these if you finish early.
Write a function called gradeCalculator that returns:
"A"for 90–100"B"for 80–89"C"for 70–79"D"for 60–69"F"below 60
Write a loop that prints all numbers from 1 to 30 that are divisible by 3.
Write a function that accepts a name and a number, then prints that name the number of times given.
Write a program that checks if a number is odd or even and then prints the result 10 times using a loop.
Answer these in comments at the bottom of your file:
- Which topic felt easiest for you?
- Which topic felt most challenging?
- How do variables, conditionals, functions, and loops work together in a program?
- What is one question you still have about JavaScript?
To complete this lab, make sure your lab.js file includes:
- variable practice
- conditional practice
- function practice
- loop practice
- at least 2 mini challenges
- reflection questions in comments
javascript-intro-lab/
│
├── README.md
└── lab.jsYou are successful in this lab if you can:
- create and use variables
- make decisions with conditionals
- write and call functions
- repeat logic with loops
- explain what your code is doing
- Read each section slowly
- Run your code often
- Fix one error at a time
- Use
console.log()to check your thinking - Do not rush — focus on understanding
This lab is about practice, not perfection.
Every developer starts with the basics. Take your time, test your code, and keep going.
Good luck.