/exercise-js-intro

An introduction to the JavaScript console and basic operators

Primary LanguageHTML

Click to review with a video walk-through.

Using the Console & Introducing Operators

Open up your console! For instructions on how to view the console in the Google Chrome Developer Tools, go here.

Some basic alerts

Let's try a quick bit of code. In that console, type the following:

alert("Hello world!");

Click enter to run that code snippet.

confirm("Cake is better than ice cream.");

Click enter to run that code snippet.

prompt("Which would win in a race,
the Millenium Falcon or the Serenity?");

Click enter to run that code snippet.

Let's do some math!

Type 3 + 5 and your should see 8 as an output.

Type 3 + 5 * 2 and your should see 13 as an output.

Note that there is an order of operations. Multiplication and division occur before addition and subtraction.

Type (3 + 5) * 2 to perform the addition before multiplying.

JavaScript File

Up to this point, all of the JavaScript you've done was in the console.

Obviously an application isn't going to work that way, a user isn't going to just type JavaScript in the console to make things happen. Anything that you type into the console are the same as actions you can perform in a JavaScript file.

Open up the file script.js in your text editor (I like using Atom.io). Open the file index.html in your browser.

Lines of code are commented out with // at the beginning of each line. To see that snippet of code run (or at least it's output in the console), remove the // to uncomment the code, then refresh your page to get the JavaScript file to run again.