but will not execute until the entire document is rendered. After the page is finished, all deferred external JavaScripts will executein the same order in which they were declared.Note: you can only declare external scripts by using thesrcattribute with a filename and path.<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Document</title><scriptsrc="myJSfile.js"defer></script><!--OR--><scriptsrc="myJSfile.js"defer='defer'></script></head><body><h1id="myH1">My Heading</h1></body></html>11
CommentsComments are “notes” that are ignored by JavaScript and are intended for humans to read instead.There are three types of comment tags used in JavaScript:1. // two forward slashes comments out everything until the end of the line2.<!--this acts exactly the same as //3. /* comments out everything until */ is found. This can span multiple lines12
VariablesVariables are like containers that can store data (values) for us. We can declare variables using thevarkeyword, and in JavaScriptwe can freely assign and change the data type.In addition, we can use thetypeof()method to check what kind of data type our variable currently holds.varx = 10;// Declaring a variable named x, equal to 10typeof(x);// Returns 'number'x ='BCIT';typeof(x);// Returns 'string', i.e. a sequence of charactersWe will see later in the class that variables can also containarrays(lists of things) and references toobjectsGetting and Setting ValuesWhen writing JavaScript, there are two fundamental concepts that we will need to use and understand:gettingandsetting.We may need, for example, to ask the browser how wide the current window is. To accomplish this, we cangetthe value bycalling one of JavaScripts built-in properties,window.innerWidth. We can choose to store this value in a variable if we needto use it later in the script, but this is optional – it is perfectly acceptable to just ask for, i.e.get, this value without storing it.Example:// 'Getting' the current window widthconsole.log(window.innerWidth);// Optionally storing the value in a variablevarcurrentWidth = window.innerWidth;alert(currentWidth);// an example of how we might later use this valueIn contrast togetting,settinga value always implies that we are using ‘=’ (called the ‘assignment operator’) to set a property.If, for example, we wanted tosetthe background color of the first paragraph on the page, we can use the assignment operator toeither store the value directly, e.g.:document.getElementsByTagName('p')[0].style.backgroundColor ='red';or we can optionally create a variable that references the first paragraph and use a combination ofgettingandsettingto set theproperty on our variable, e.g.:// Using document.getElementsByTagName() to get a reference// to the HTML element and storing it in a variablevarfirstParagraph = document.getElementsByTagName('p')[0];// We can now use firstParagraph to set properties on our element,// rather than having to repeatedly write document.getElementsByTagName('p')[0]// Setting the backgroundColor property of our variable,// (a reference to the element on the page) to redfirstParagraph.style.backgroundColor ='red';13
We can also use a combination of getting and setting in one line if we do not need to create variables for use later in the script. To