Back again for some more fun with JavaScript. I will eventually lead this conversation into a website project and maybe some off shoots into areas of gaming or server scripting. However before you can run you must first learn to walk so here we are. There are some basic things you must know about any programming language there are rules and syntax that the interpreter understands and if you break these you will not have a functional program. Everyone assumes to be able to program you need to be good at math. I received my bachelors degree in Mathematics and my master's degree in Information Systems. However useful the math is for logic and data I've found programming more suited to learning a new language.
Types
There are five primitive data types in JavaScript and they are as follows:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Number // Example 32, 32.1 | |
String // Example 'hello world', "hello world" | |
Boolean // Example true, false | |
Undefined // Where the object is not defined or assigned properties | |
Null // Like darkness is the absence of light Null is the absence of data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object //Example {x:1,y:3} | |
array //Example [1,2,3] | |
function //Example function functionName(parameters){ //code to execute} |
Comments
There is some debate on whether you would want to use comments in code at least a few articles I have read lately they debate that well written code needs not to be cluttered with comments. I myself find them very useful. If you have a complex piece of code you would want other developers to know what a certain section of logic does especially when you are using shortened naming conventions as is common in JavaScript to keep payload down. Here are examples of how to use comments and you will notice up above I have already left a few in prior examples.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the double hashes are for single line comments. | |
/* | |
* is also recognized as a comment for single lines as well as multiple lines for each line between the start and the finish you merely add another * per line. | |
* between the start and the finish you merely add another * per line. | |
*/ |
Keywords
There are a few words that are not allowed to be used as literals in your syntax because the interpreter regards these handful of words to perform operations. An example would be a variable. Just like your college algebra class you had to solve the following equation x+ 4 = 8 so here naturally x would be equal to 4 to complete the expression. In programming we have placeholders such as x to hold values or objects these are called variables. A variable definition would go as follows in the code:
var x = 4;
Here we are declaring a variable name x and it hold the value 4 which can be reused or reassigned throughout the code. The var portion of the variable declaration is a keyword. The interpreter knows that following the var keyword an a variable name then after and equal sign knows that a value is going to be assigned to the variable which has been declared. Here are some more keywords that are worth noting I would recommend visiting the following link for the functionality and definitions of what these key words do.
Your First Program
First we need to have a place to write and execute our code luckily with JavaScript we will not need expensive IDE just need to install Firefox.
Download Firefox
To open a scratch pad just click on the orange Firefox menu in the top left corner of the browser and then select Web Developer = > Scratch pad
This will allow you to write some basic JavaScript then execute it in the browser. Here comes the boom first program that every developer needs to write is the "Hello World!" program so in your scratch pad perform the following:
You will find that this opens in the browser and congratulations you are starting to code like Vikings. I'll catch you next week with some more fun.
Download Firefox
To open a scratch pad just click on the orange Firefox menu in the top left corner of the browser and then select Web Developer = > Scratch pad
This will allow you to write some basic JavaScript then execute it in the browser. Here comes the boom first program that every developer needs to write is the "Hello World!" program so in your scratch pad perform the following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//declare a variable with the string literal | |
var message = 'hello world'; | |
/* | |
* Use built in function alert and pass the variable as a parameter to this function. | |
* this should open a dialog window with your message. Also you will notice the comments made that are | |
* ignored by the interpreter. There are only two lines in this snippet it cares about. | |
*/ | |
alert(message); |