• Introduction to Javascript

    This article will introduce you to Javascript and help you get started with using Javascript to write messages to the documents.

    Javascript is a scripting language that can be used to apply more interactive code to your website, it also makes accessing objects within a page much simpler.

    Javascript can either be placed in a file with a .js extension or placed within html using the script tags.

    HTML Code:
    <script type="text/javascript">
    document.write("My first piece of Javascript!");
    </script>
    The example piece of code above will print the text within the brackets to the users screen. We can use the same piece of code to print variables to the users screen as well.

    HTML Code:
    <script type="text/javascript">
    var firstValue = "test1";
    var secondValue = "test2";
    	document.write(firstValue + ' - ' + secondValue);
    </script>
    The above code would output:

    test1 - test2

    Within the document.write you will notice there are two plus signs, this indicates that after the variable there is more content to follow, if we just wished to print the two variable contents without the hyphen then we would only need one plus and no single quote.

    Try saving the following as a .html file and see if you can change the inputs and outputs to make sure your confident with printing variables and text.

    HTML Code:
    <html>
    <body>
    
    <script type="text/javascript">
    var firstValue = "This is a test";
    var secondValue = "to check I have learnt well";
    	document.write(firstValue + '  ' + secondValue);
    </script>
    
    </body>
    </html>
  • Stay Updated