JavaScript in external file (optional)

For a more easy overview of your HTML, you can copy all the code that is in the script tag to a new file and for example call it chat.js.

To use it in your code, you have the link to the script just before you end the body tag.

        body
          (...)

          <script src="chat.js">script
        body
    

Writing and testing JavaScript in the browser

To be able to print and read results from your code, you have to open up a console. On Chrome you can access this by clicking on 'View' > 'Developer' > 'Developer Tools'.

Now that we are working on Glitch, your window looks probably like this:

  __________________________________
  |          |          |          |
  |          |          |          |
  |  code    | preview  | Chrome   |
  |  editor  | window   | console  |
  |          |          |          |
  |          |          |          |
  |          |          |          |
  __________________________________
  

Data types

In JavaScript all data is stored in different formats:

          number = 123;
          string = "welcome back to awww!";
          boolean = true or false;
    

Variables

To store data as an object with a name, you create a new variable:

    var example; // variable is accessible on the whole page
    let example; // variable is only accessible within the same code block
    const example; // variable is only accessible within the same code black and can not be changed after getting it's initial value
    

Functions

You can organise and reuse code by bundling bits of code in a function.

  function myFirstFunction() {
  
  }
  

This is a function that can multiply two numbers. The input numbers are passed to the function via arguments, the values placed within the '( )';

  function multiply(num1, num2) {
    let result = num1 * num2;
    return result;
  }
  

So far we only defined the function, to execute it, we call it as follows:

  multiply(5, 10); // this will return 50 in the console