JavaScript

JavaScript can be used to make your page more interactive for the user using a variety of functions. Functions are the basic building blocks of JavaScript and they are a way of packaging functionality that you wish to reuse in your code. Nested functions could have one or more inner functions. The outer functions cannot access variables that are defined inside of the inner functions but inner functions can access variables in outer functions. Variables in codes are used as a form of symbolic name for values in an application. Also known as identifiers, the variable names conform to certain sets of rules and must have a unique name.

An "if-else" statement is a very common form of conditional used to test if a true or false expression is returned. It allows you to make decisions directly in your code and can even control the flow of the program based on the set conditions. An array is a structure used to store a collection of data values and can be various data types.

Click on the links to the left to begin learning!

Variables

Variables can be used as containers that store values, for example: myName = "John".

In this variable, you are assigning the string "John" to the myName variable.

Example image of deployed JavaScript code

Here's what the code looks like:

var myName = "John";
var message = "Hello world! My name is ";
console.log(message + myName + ".");
                    

Functions

Functions are a good alternative to writing the same code repeatedly since they can be used multiple times.

This allows us to make our code more organized and more readable.

Example image of deployed JavaScript code

Here's what the code looks like:

function addNumbers(num1,num2,num3) {
    var result = num1 + num2 + num3;
    return result;
}
var result = addNumbers(3, 5, 7);
console.log(result);
                    

If Else Statement

For the "if" part, if the condition is in fact true, the code inside that block is executed.

If it is false then it is skipped and goes to the "else" part. This is where an alternative set of instructions would be executed since the initial statement was not met.

Example image of deployed JavaScript code

Here's what the code looks like:

var x = 100;
var expression1 = (x > 25);
var expression2 = (x < 50);
if(expression1 && expression2) {
    console.log('True True');
    } else if(expression1) {
    console.log('True False');
    } else if(expression2) {
    console.log('False True');
    } else {
    console.log('False False');
}
                    

Arrays

You would declare the arrays in JavaScript and give them a name.

For example, if you want to create a list of vegetables, the array could be given a variable name of "vegetables" and the items would be listed out.

Example image of deployed JavaScript code

Here's what the code looks like:

                    var vegetables = ["lettuce", "onion", "cabbage", "potato", "carrot"];
                    var message = "I need to buy: "
                    console.log(message + vegetables + ".");