Author Topic: Javascript Tutorial 2 - First Codes  (Read 7338 times)


  • Citizens
    Voting Member
  • Posts: 255
  • Karma: 2
    • View Profile
    • Awards
Javascript Tutorial 2 - First Codes
« on: April 30, 2011, 09:57:35 PM »
This is a continuation of my previous tutorial, "Javascript Tutorial 1 - Beginner's Guide".

---Chapter 1---
~~Code Structure~~
Code,specifically Javascript code, uses an easily memorized structure. The structure comes in 4 general parts:
<html> - This goes first, so that you can write in HTML (Hyper Text Markup Language)
<head> - This goes below <html> and is used for off-screen stuff (CSS, JSS, HTML, Etc)
<body> - Most of the writing will be done in here
<script type="text/javascript"> - This is the most important tag for Javascript coding, since it lets the computer/browser know that everything following this is Javascript related.

Drum these tags into your thick skulled head, since these are the most important parts.

---Chapter 2---
~~Advanced Commands~~
Some of the more advanced commands will be listed and explained here:
Math.random()*Number Here - This is a slightly lengthy line of code that picks a random number. It will pick any number between 0 and "Number Here"

Math.floor() - This is an addition to "Math.random" that rounds the number down to the nearest whole number. Take the number you want and add 1

Math.ceiling() - This is an addition to "Math.random" that rounds the number up to the nearest whole number. Take the number you want and subtract 1

Code: [Select]
var randomnumber = Math.floor(Math.random()*11);

Break it down: This generates a random number between 0-10 and then writes it in your browser.

<img src=\"Enter image URL here"> - This line of code makes the selected picture appear.
Code: [Select]
document.write("This is me!")
<img src=\"URL for a picture of me">

---Chapter 3---
~~Making a code~~
One example of a code is:

Code: [Select]
<script type="text/javascript">
var variable=5

document.write("Variable is equal to 5!")

This specific piece of code contains almost all of the points discussed in my last tutorial (Excluding the "else" statement). Now to break it down:

The "openers", or the <html>,<head>,<body>, and <script type="text/javascript"> let your browser/computer know what is going to happen (Re-read Chapter 1, Code Structure).
var variable=5 - Creates a variable, or "shortcut" that is the number 5.

if(variable==5) - This is part of an "if/then" statement. IF the variable (5) is equal to (==) 5....

{document.write("Variable is equal to 5!")} - THEN it writes this.

Then "enders" or </script>,</body>,</head>, and </html> means that the program/code is done, and that it has to stop.

This may seem like a lot, but after only 2 or so weeks, I can write this without thinking about it.

Various Google sources

Coding Tools:
Notepad++ (Over 40 different code languages!)
Other items all over the internet

As always, corrections and comments are appreciated.

Edit 1: Fixed some errors
« Last Edit: July 19, 2013, 01:41:46 PM by Jubal »

Son of the King

  • Megas Domestikos
    Voting Member
  • Posts: 3368
  • Karma: 29
  • Awards Awarded for outstanding services to Exilian!
    • View Profile
    • SotK
    • Awards
Javascript Tutorial 2 - First Codes
« Reply #1 on: May 01, 2011, 11:27:30 PM »
Just one point, the closing tag for head usually appears before the body tag like this:

Spoiler (click to show/hide)

This is just a convention though and the way you described still works, just looks less neat to me...
« Last Edit: May 05, 2011, 11:39:52 PM by Son of the King »


  • Devourer of Souls
  • Megas Domestikos
    Voting Member
  • Posts: 1667
  • Karma: 10
  • I think, therefore I am.
    • View Profile
    • Awards
Javascript Tutorial 2 - First Codes
« Reply #2 on: May 02, 2011, 12:54:42 AM »
The head tag has a big purpose, it holds all html/css/js info that isn't displayed on the screen, or is not directly to do with the look of the page.
"So if you meet me, have some courtesy, have some sympathy, and some taste. Use all your well learned politesse, or I'll lay your soul to waste."