26
loading...
This website collects cookies to deliver better user experience
Who is this tutorial for?
Everything here is for total beginners – you don't need to know anything about programming, web development or JavaScript.
In this tutorial, we'll make sure our code is working by logging messages there.
If you're using Chrome, you can open the console by pressing Option + ⌘ + J (on macOS), or Shift + CTRL + J (on Windows/Linux).
To learn more, check out this video.
Browser
A browser is a program your computer uses to open a webpage (you probably know this because you're using one right now to read this sentence). Examples of browsers are Chrome, Firefox and Safari to name a few.
There's an empty CodeSandbox interface at the bottom of this article you can use to practice along with each exercise.
You can also create your own CodeSandbox on their website if you want to save your program and reopen it later. Checkout CodeSandbox here.
Once your CodeSandbox is set up, delete everything in index.js to start with a clean slate.
Analogy
Think of functions as little factories. You turn them on and they spit out a desired result on command.
logName();
Analogy
Think of the parens on the end of the function name like a red ball on the end of a lever. In order to start our factory (call our function), we need to PULL on that giant lever.
Adding the parens to the end of the function name is how we pull that lever and start our factory running.
Important
When you write function or variable names, capitalization counts. logName is not the same as LogName.
function logName(name){
console.log(name);
console.log('5 Slacker Ave')
console.log('Los Angeles, CA 10001')
}
logName('Donny');
// Donny
// 5 Slacker Ave
// Los Angeles, CA 10001
Analogy
The parentheses in a function declaration act as a kind of loading dock for our factory. Anything we load in through those parentheses we can then use inside our factory.
Important
We can name our parameters whatever we want. If we want to use that parameter in the function's body however, we need to match that name exactly. Just like with our function names, spelling and capitalization count.
function logName(orange){
console.log(orange);
console.log('5 Slacker Ave');
console.log('Los Angeles, CA 10001');
}
logName('Walter');
function logAddress(name, street, cityStateZip){
console.log(name);
console.log(street);
console.log(cityStateZip);
}
logAddress('Walter', '10 Aggression Ave', 'Los Angeles, CA 10001')
// Walter
// 10 Aggression Ave
// Los Angeles, CA 10001
Write a function that logs the name of your first pet, type of animal and their favorite toy.
Update the function so it uses parameters and arguments
// Question 1
function logPet() {
console.log("Mac");
console.log("Dog");
console.log("Bone");
}
// Question 2
function logPet2(name, animal, toy) {
console.log(name);
console.log(animal);
console.log(toy);
}
logPet();
// Mac
// Dog
// Bone
logPet2("Raptor", "Iguana", "Warm rock");
// Raptor
// Iguana
// Warm rock