Javascript 1

To make things happen on a web page we need to write some javascript code.
Here is the code that said 'hello' when you first loaded this page:

            function hello(){
                alert("hello");
            }
            
            hello();
            

Understanding Javascript

The word function creates a block of code we can use whenever we wish.

The name hello is a name I chose, because the code says 'hello'! 😄

Next we have two round brackets: ( and ).

Inside the curly brackets: { and }, we write the code that tells the computer what to do.

In this example we use the method alert which puts a message box on the screen.

Inside alert's round brackets: ( and ), we write our message.

We put our message in quote marks like this "Write your message in quotes!"

At the end of the line we put a semi-colon: ;

The code won't do anything at all until we call it.
We do that by writing:
  1. the name of the function: hello,
  2. two round brackets: ()
  3. and a semi-colon: ;

When the page is loaded the browser gets to the code hello(); and runs it.

So we see the message on the screen.

Try it out yourself!

On your web page you will need to write <script>, then write your javascript and then write </script>

Like this:

            
            <script>
                Write your javascript here
            </script>
            
            

Your code will look a lot like mine. There are some things you can change:

There are some things that you can't change: javascript won't work without them!

Please ask for help if you get stuck.

Good luck! 😄