When you declare a new variable, you're telling the program that this is a NEW variable that you will use later.
let a;Not to be confused with define, you don't actually give the variable a value if you just declare it.
When you assign a value to a variable, or when you define a variable, you give it a value (or change its existing value).
a = 12;💡 You can also combine them!
let a = 12;This declares a variable a AND assigns a value of 12 to it.
Variables are used to store and manipulate data. With variables, you are using labels to point to the data rather than using the data itself.
Imagine variables are empty buckets that you can store things (data) inside.
They are called variables because the information they represent can change but the operations on the variable remain the same. Consider this example:
let myFavoriteFruit = "apple"
function eatFruit(fruit) {
console.log(" I'm eating", fruit)
}
eatFruit(myFavoriteFruit); // This will print “I'm eating apple”
myFavoriteFruit = "orange" // The data that myFavoriteFruit stores has been changed from “apple” to “orange”
eatFruit(myFavoriteFruit); // This will print “I'm eating orange”Throughout the program, the function eatFruit() doesn't change when we call it with myFavoriteFruit. It will always prints out "I'm eating + [the content that myFavoriteFruit holds]‘', even though the content thatmyFavoriteFruit hold may change.
Test your understanding with the below challenge:
Declare 3 variables called myNumber, myName, mySchool
Requirements:
ABCCoderSchoolExample:
const myVariables = 'value';
let userName;let constThere are 3 main ways to declare or create a variable. let const var. Keep in mind var is old school, while let and const are more recommended.
There are also uninitialized variables, which is just a variable that has been declared but has not been assigned a value when it was declared.
Test your understanding with the below challenge:
Declare 3 variables called myNumber, myName, mySchool
Requirements:
myNumber stores a boolean value: truemyName can't be assigned again and stores a string value: abc123mySchool stores no values and can be re-assigned a new value later.Example:
const myVariables = 'value';
let userName;Test your understanding with the below challenge:
Requirements:
Declare 3 variables called myId, myComputerId, myWorkId
myId store a number value: 123myId can't be changed.myComputerId and myWorkId to myId so that they share the same data.myComputerId and myWorkId can be changed.Example:
const password = 123;
let passwordHouse = password;
let passwordPhone = password;Test your understanding with the below challenge:
Declare 2 variables called userName and shoppingCart
Requirements:
userName stores a string value: AliceuserName can't be changed.shoppingCart store a value: appleshoppingCart be overwritten with a value: bananaExample:
const password = 123;
let passwordHouse = password ;
let passwordPhone = password ;
passwordPhone = `phone123`;We have learned about the meaning of functions in programming.
How to create and call your functions?
What are inputs?
What are outputs?
Read more on function.
Test your understanding with the below challenge:
Requirements:
Create a function named printName().
printName() print “CoderSchool” to the console.printName() does not take inputs.Example:
function printDate() {
console.log('22/02')
}
printDate();Inputs or Arguments: Real values that are passed into the function. Inputs or Arguments are defined when you call a function.
Parameters: Labels that act as placeholders for the real values that your functions need to run. Parameters are defined when you create a function.
Test your understanding with the below challenge:
Requirements:
Create a function name printStudent():
printStudent() takes two inputs name age.printStudent() outputs name and age to the console.Example:
function printFunction(param1,param2) {
console.log(param1,param2)
}
printFunction('Alex','Nguyen');Input is what the function takes in, output is what the function returns .
After passing values through a function, if you don't tell the function to return the result, you won't be able to “see” or get the output.
Imagine going to the laundry service, if the service doesn't return the washed clothes (the processed data) to you, you won't be able to wear your clean clothes (to use the data processed by the function).
Test your understanding with the below challenge:
Requirements:
Create a function named plusThree(num).
plusThree() takes 1 input numplusThree() returns a value of num + 3plusThree() is called and passed in a value of your choice.plusThree()'s output is assigned to the variable answer.answer can be changed over time.answer is logged to the console.Example:
function double(num) {
return num * 2;
}
let storage = double(6);
storage = double(12)
console.log(storage)In JavaScript, scope refers to the reachability of variables.
Variables that are not declared inside any function block have Global scope. This means they can be used everywhere in your JavaScript code.
Variables that are defined inside of a function block have Local scope. This means they can only be used inside that function.
Test your understanding with the below challenge:
Requirements:
Create a function named calculator(), 2 global variables a and answer.
a is a constant and can't be changed. a is assigned a value: 10.calculator() takes an input numcalculator() has a local variable b,b has value of : 5calculator() returns a result of a + b + numanswer is assigned the output of calculator()answer is logged to the console.const global = 5;
function addNumber(num) {
let local = 3
return global + local + num;
}
let result = addNumber(6);
console.log(result)
//expected result: 14Variables that are declared with var automatically have global scope, even if it's declared inside a function. This can create unintended consequences elsewhere in your code or when running a function again. var is not recommended!
You should always declare your variables with let or const.
Test your understanding with the below challenge:
Requirements:
Create a function named plusA() and a variable a
a is a constant and can't be changed.plusA takes 1 input numplusA returns an output that equals num + aplusA is called and passed in a value.plusA's output is assigned to the variable answeranswer is a constant and can't be changed.answer is logged to the console.Example:
const constantNumber = 5;
function triple(num) {
return num * constantNumber;
}
const result = triple(3)
console.log(result)
//expected result: 15a = a + b .In programming, we can use the assignment operator ( = ) to change the value of a variable.
Note that everything to the right of = is evaluated first. Therefore, for a = a + b, the value current value of a and the value of b are added together and the result of that is then assigned to the variable a.
Test your understanding with the below challenge:
Requirements:
Declare a variable myNumber.
myNumber is assigned a value: 1myNumber is assigned a new value that includes the original value + 5: myNumber + 5myNumber is logged to the console.Example:
let a = 1;
a = a + 7
console.log(a)
//expected result: 8a += bThis is another way to write a = a + b. With +=, we basically do both adding and assigning at the same time.
Test your understanding with the below challenge:
Requirements:
Declare a variable num.
num is assigned the value: 2num is assigned a new value that includes the original value + 7: num + 7num is logged to the console.Example:
let a = 1;
a += 7;
console.log(a)
//expected result: 8a -= bThis is another version of a += b but with -.
Test your understanding with the below challenge:
Requirements:
Declare a variable smallerNumber.
smallerNumber is assigned the value: 2smallerNumber is assigned a new value that includes original values - 2: smallerNumber - 2smallerNumber is logged to the console.Example:
let a = 10;
a -= 2;
console.log(a)
//expected result: 8"Hello" + "friend!"Operator “+” is also used to combine strings.
Test your understanding with the below challenge:
Requirements:
Declare a variable myName.
myName is assigned a string value: ‘Alex'.myName is assigned a new value including: original value + string: NguyenmyName is logged to the console.Example:
let mySchool = 'Coder';
mySchool += 'School';
console.log(mySchool)
//expected result: CoderSchoolTemplate literals use back-ticks (``) to wrap around a string instead of the regular quote (“ ”). The position of the backtick key may vary on different keyboards, but it looks like this:

Template literals provide an easy way to perform string interpolation - which is to interpolate (insert) the values of variables and expressions into a string. The syntax is ${variableName}
Example:
let pet = "cat"
console.log(`I have a ${pet}`) // Prints "I have a cat"Test your understanding with the below challenge:
Requirements:
Declare function createLearner() and a variable school.
school is a constant and assigned a value: CoderSchholcreateLearner() takes 2 inputs: name agecreateLearner() returns a string: (Name) is (age) years old at (school)Example:
let userAddress = 'HCM';
function createStudent(name,age) {
return `${name} is ${age} years old at ${userAddress}`
}
let answer = createStudent('Alex',26);
console.log(answer);
//expected result: `Alex is 26 years old at HCM`IF...ELSEIf...else is used to make decisions in code. If a condition is true do action A, else - condition is false do action B.
Test your understanding with the below challenge:
Requirements:
Declare an If..else statement, 2 variables a, b
a > bconsole.log("a is bigger than b.")console.log("a is smaller than b.")a is assigned a value: 5b is assigned a value: 10Example:
let num1 = 1;
let num2 = 5;
if (num1>num2) {
console.log("num1 is bigger than num2.")
} else {
console.log("num1 is smaller than num2.")
}If chaining - if ..else ifUsually, there will be more than just 1 condition is needed in code. else if will provide you with the second condition.
Test your understanding with the below challenge:
Requirements:
Declare an If..else statement, 2 variables a, b
a > ba === bconsole.log("a is bigger than b.")console.log("a equals b.")console.log("a is smaller than b.")a is assigned a value: 10b is assigned a value: 10Example:
let num1 = 5;
let num2 = 5;
if (num1>num2) {
console.log("num1 is bigger than num2.")
} else if (num1 === num2) {
console.log("num1 equals num2.")
} else {
console.log("num1 smaller than num2.")
}Most of the time, If will not be used independently but rather inside a function.
Test your understanding with the below challenge:
Requirements:
Create a function testHeight() that helps recognize if the input is smaller, bigger, or equal to height.
testHeight() takes 1 input numtestHeight(), declare a variable height with value 100.testHeight(), write an if statement.(num > height)(num === height)'num is larger' in the console.'num is equal' in the console.'num is smaller' in the console.testHeight() with an argument of your choice.Example:
function compareNumber(num) {
let a = 50;
if (num > a) {
console.log("num is bigger than a.")
} else if (num === a) {
console.log("num equals a.")
} else {
console.log("num smaller than a.")
}
}
compareNumber(100);ifRequirements:
Write a function to test if a number is greater than or equal to 10, and is an even number. The function returns You found the right number! if two conditions are met.
Hints: % - remainder.
testNumber() takes in 1 input num.(num >= 10);Number is smaller than 10 if 1st condition is false.(num % 2 === 0);You found the right number! if 2nd condition is true.Number is not an even number. if 2nd condition is false.result to the output of testNumber()result to the console.Example:
function findNumber(num) {
if (num >= 50) {
if (num % 5 === 0) {
return 'Right number!'
} else {
return 'Wrong number!'
}
} else {
return 'You number is smaller than 50 '
}
}
let answer = findNumber(100)
console.log(answer)FOR LOOPfor loopLoops help you run the same code over and over again, each time with a different value. And the for statement is a way to create loops.
Reference: read about for statement
Test your understanding with the below challenge:
for ([initialization]; [condition]; [final-expression]) {
statement
}Requirements:
Create a loop to log numbers to the console.
let i = 0;i < 9;i++console.log(i)Example:
for ( let i = 1 ; i < 5 ; i++ ) {
alert(i)
}for loop with arrayMost of the time, loop is used to apply the same code to every single element of an array.
Test your understanding with the below challenge:
for ([initialization]; [condition]; [final-expression]) {
statement
}What is array.length()?
Requirements:
Create a loop to log all elements of the array to the console.
toys that has value: ['kite', 'car', 'bike']let i = 0i < toys.lengthi++Example:
let array = ['zero','one','two','three']
for ( let i = 0 ; i < array.length ; i++ ) {
console.log(array[i])
}for loop, array, and function.Test your understanding with the below challenge:
for ([initialization]; [condition]; [final-expression]) {
statement
}Requirements:
Create a function to loop over all elements of the array and add them to a string.
numbers that has values [‘one',‘two',‘three',‘four',‘five']sum has value '' (empty string);sumArray(), takes in 1 input arr, and outputs sumsumArray() has a for loop.let i = 0i < arr.lengthi++sumArray(numbers)sum to the console.Example:
let a = ['zero','one','two','three']
let result = '';
function sumArray(array) {
for ( let i = 0 ; i < array.length ; i++ ) {
result = result + array[i];
}
}
sumArray(a)
console.log(result)for loopSometimes you wil have arrays inside an array and loops inside of a loop.
Another cool name for arrays inside an array is 2D Array (D stands for ‘dimensional)'. Loops inside a loop can be called Nested loops
Test your understanding with the below challenge:
let array = [[0,1,2],[0,1,2],[0,1]]
for ([initialization1]; [condition1]; [final-expression1]) {
for ([initialization2]; [condition2]; [final-expression2]){
statement
}
}Requirements:
Create a nested for loop to loop over a 2D array.
a has values [[0,1,2], [5,10], [2,4,6]]for loop.let i = 0i < a.lengthi++console.log(a[i][j])Example:
let array = [['1','a','b','c'], ['2','a','b'], ['3','a','b','c']]
for ( let i = 0 ; i < array.length ; i++ ) {
for (let j = 0; j < array[i].length; j ++) {
console.log(array[i][j])
}
}for loop , function, returnLeveling up your looping skill with function and return
Test your understanding with the below challenge:
let array = [[0,1,2],[0,1,2],[0,1]]
for ([initialization1]; [condition1]; [final-expression1]) {
for ([initialization2]; [condition2]; [final-expression2]){
statement
}
}Requirements:
Create a function to go through a 2D array, with for loop and return a value. This value is the sum of all numbers in the array.
a has values [[5,10,15], [5,10], [5,10,15]]answer, no value assigned.sumArray() takes 1 input array, output 1 value sum.sumArray(), declare variable sum and assign value 0.for loop inside sumArray()let i = 0i < array.lengthi++sumArray()'s output to answeranswer to the console.75 is expected to be logged.Example:
let array = [[5,10,15], [5,10], [5,10,15]]
let result;
function multiplyArray() {
let multiply = 1;
for ( let i = 0 ; i < array.length ; i++ ) {
for (let j = 0; j < array[i].length; j ++) {
multiply = multiply * array[i][j];
}
}
return multiply;
}
result = multiplyArray(array)
console.log(result)