<script> tag either the head or body elements of an HTML document.
<script> document.write("Hello world!"); </script>
Here, we're writing the text "Hello world!" to the document. We'll learn all about the document object and its functions in the Document Object Model section.
If you plan on placeing JS code directly in your body, it's best to place it just before your
</body> closing tag.
This will improve page loading time since no HTML will be blocked by any of your scripts loading.
To link externally, make your external file's extension .js and link it to your document with the src attribute:
Literals are fixed values of either numbers (with or without decimals), or strings.
// numerical literals 3.14; 48; // Strings "Hello World!"; 'Hello Mars!';
Literals won't do much on its own, so let's take a look at variables, which are containers that can store literals.
Variables store data values such as literals. They can also be set to expressions, which are evaluated and placed into the variable.
To define a variable, we use the
var keyword with the assignment operator
// Initialize variable count and set it to 0 var count = 0; // Or just initialize the variable var count;
In the second initialization, the variable
count is initalized to
Declaring Multiple variables
You can also declare multiple variables while using the
var keyword just once.
var price = 5, quantity = 23, total = 50; var price, quantity, total;
A loosely typed language
var keyword. We'll talk about the different data types and typecasting in the next section.
Guidelines for variable
Here are some guidelines to follow when assigning a variable name:
- Variable names cannot start with a number.
- Valid characters only include letters, numbers, $ and _.
- Variables are case sensitive!
When deciding on a variable name, it's important to be clear and descriptive. Names like
pre are too short and don't convey any information.
Variables of more than one word should have a upperCamelCase format. This means that the first word is all lowercase, and any subsequent words have their first letters capitalized. For example, a variable that holds the number of remaining books would look like
Statements and Code Readability
As the proper developer, you should separate each statement with a semicolon.
Statements should be placed in blocks for organization and clarity.
Remember that programming is not just about making things work, but allowing others to read and understand your code. This often means that you should find a good middle-ground between a code's efficiency and readability.
Comments are also helpful to prevent execution of part of a code when you're trying to debug. Simply comment out the part you don't want to run.
// Comments precede a double slash /* Or go between the slash asterick notation like so */
var greeting = "Hello"; var greeting="Hello" ;
It's better to opt for the former, which allows for better readability.
Three ways to print
Writing out as an alert
An alert window is a small window popup. We can display text on it with the
Writing onto document
To write onto our document (or within the browser), we use the
document.write() function. We'll learn all about the
document object in an upcoming lesson.
Writing out to console
Thus, we have three ways to output text:
alert("Hello world!"); document.write("Hello world!"); console.log("Hello world!");
As you go through this tutorial, it's important to try out what you learned on your own, and use one of the three ways to check your outputs.