Ok, this tutorial will help those who have no background programming knowledge learn Javascript from the very basic.
1. Setting up environment.
There are many JS editors out there but my favorite is Firefox Developer Edition not because it is free, also has many features supported such as CSS, HTML5 and other tools to debug front-end web applications. You need some configuration to get your JS environment ready for learning purpose.
After downloaded and installed, have your Firefox Developer opened and press Ctrl+Shift+I to open developer tools. Click on setting icon on the far right of developer tool, check Scratchpad, the Scratchpad icon will appear on the developer tool. Click on the icon open Scratch Pad. Under developer tools, click on Console tab.
You will see something similar like this picture.
2. Gettting started with programming in JS (Javascript).
Type in your Scratchpad.
1 var playerName = "Ralph";23 if (!playerName){4 console.log("inside if");5 }else{6 console.log("inside else");7 }89 playerName = "Robo";1011 console.log(playerName);Click on Run button on top of your scratchpad. You will see something like this under Console.
inside elseRoboWhat you just did is programming in JS. Let me explain how JS programming works. After you press Run, the computer reads the very first line and then come down to second line and keep going until it hits no more line or hits a line which tells it to stop running (reading).
The example above, computer reads line 1 first. What this line did is declare a variable. Variable is a place holder for your data. Thinking of it as a box where you stuff your stuffs in that box for for later use. The var keyword in front of playerName is basically tells the computer that hey I want to declare this as variable and I name it playerName. The computer reads the var keyword and does some calculations to preserve memory location for that variable. You don't need to know what computer does behind the scene, you only need to know that, you tell computer you declare a variable with the name playerName.
The computer then reads line 2. In this example line 2 has nothing written down, so computer does nothing, will read to next line 3. This third line the computer reads if keyword, this keyword tells computer start evaluate what inside the parenthesis (!playerName) which I will explain later, you only need to understand how computer reads JS for now. If the inside that parenthesis returns keyword true the computer will read line 4. If return keyword is false, computer will skip line 4 and jump to read line 6. In line 6 basically tells computer to write the words inside else to the screen, in the Console tab. The computer then continue read line 8, 9 ...11 write on the screen the word Robo, end of script.
Ok, simple right. It is not that hard when you know the basic.
2. Variables
Type this in your Scratchpad
// 5 primitive types.String : var playerName = "Ralph";Boolean : var eventSwitch = true;Number : var playerHP = 34;Null : var playerSprite = null;Undefined : var playerMP; // when define variable with no value, undefined will automatically assign.// reference typevar sprite = new Object();In the example above, you wrote instruction to computer by telling the computer that you declare some variables with the var keyword, follows variable's names. The equal sign = tells computer you want the value to be assigned to that variable. You can think of variable like a box (place holder) and you need to put stuffs in it otherwise what the point of having the box for!. So on the right side after = those are the values .
However in JS, every variable has a type associate with that variable. This similar to every box can store the same type of stuffs. There are 6 different types of variables, 5 primitive types and reference type. The 5 primitives types are ; string, boolean, number, null, undefined. The reference type is an object. The different between primitive type and reference type is primitive type stores data while reference type (object) stores reference of object (pointer to memory location) which will explain later.
String type is actually a string of characters declare inside of double quotation marks " " or single quotation marks ' ' . Everything inside the marks computer will understand as string value.
Boolean type is either true or false represents to an on or off switches in game. That because computer has only two states 1 and 0. 1 is on and 0 is off. In programming language, these two states represent by boolean true or false . It is important to understand this concept because most of programming instructions written for computer return one of these two values.
Number type is of math numbers (integers, float, decimal...etc.) and to differentiate with string value it is written without marks. So the computer can differentiate between string of for example "34" and math number of 34.
Null type is empty value preserve for another value to be assigned to that variable. When you declare variable with null type, you basically tell the computer preserve that memory location for your variable however you have not decided to assign any computation value into that variable yet.
Undefined type is similar to writing instruction but you don't tell computer preserve any memory location for it. The computer don't know the existence of that value. So whenever you get "undefined" from computer means computer don't know anything about it.
1. Setting up environment.
There are many JS editors out there but my favorite is Firefox Developer Edition not because it is free, also has many features supported such as CSS, HTML5 and other tools to debug front-end web applications. You need some configuration to get your JS environment ready for learning purpose.
After downloaded and installed, have your Firefox Developer opened and press Ctrl+Shift+I to open developer tools. Click on setting icon on the far right of developer tool, check Scratchpad, the Scratchpad icon will appear on the developer tool. Click on the icon open Scratch Pad. Under developer tools, click on Console tab.
You will see something similar like this picture.
2. Gettting started with programming in JS (Javascript).
Type in your Scratchpad.
1 var playerName = "Ralph";23 if (!playerName){4 console.log("inside if");5 }else{6 console.log("inside else");7 }89 playerName = "Robo";1011 console.log(playerName);Click on Run button on top of your scratchpad. You will see something like this under Console.
inside elseRoboWhat you just did is programming in JS. Let me explain how JS programming works. After you press Run, the computer reads the very first line and then come down to second line and keep going until it hits no more line or hits a line which tells it to stop running (reading).
The example above, computer reads line 1 first. What this line did is declare a variable. Variable is a place holder for your data. Thinking of it as a box where you stuff your stuffs in that box for for later use. The var keyword in front of playerName is basically tells the computer that hey I want to declare this as variable and I name it playerName. The computer reads the var keyword and does some calculations to preserve memory location for that variable. You don't need to know what computer does behind the scene, you only need to know that, you tell computer you declare a variable with the name playerName.
The computer then reads line 2. In this example line 2 has nothing written down, so computer does nothing, will read to next line 3. This third line the computer reads if keyword, this keyword tells computer start evaluate what inside the parenthesis (!playerName) which I will explain later, you only need to understand how computer reads JS for now. If the inside that parenthesis returns keyword true the computer will read line 4. If return keyword is false, computer will skip line 4 and jump to read line 6. In line 6 basically tells computer to write the words inside else to the screen, in the Console tab. The computer then continue read line 8, 9 ...11 write on the screen the word Robo, end of script.
Ok, simple right. It is not that hard when you know the basic.
2. Variables
Type this in your Scratchpad
// 5 primitive types.String : var playerName = "Ralph";Boolean : var eventSwitch = true;Number : var playerHP = 34;Null : var playerSprite = null;Undefined : var playerMP; // when define variable with no value, undefined will automatically assign.// reference typevar sprite = new Object();In the example above, you wrote instruction to computer by telling the computer that you declare some variables with the var keyword, follows variable's names. The equal sign = tells computer you want the value to be assigned to that variable. You can think of variable like a box (place holder) and you need to put stuffs in it otherwise what the point of having the box for!. So on the right side after = those are the values .
However in JS, every variable has a type associate with that variable. This similar to every box can store the same type of stuffs. There are 6 different types of variables, 5 primitive types and reference type. The 5 primitives types are ; string, boolean, number, null, undefined. The reference type is an object. The different between primitive type and reference type is primitive type stores data while reference type (object) stores reference of object (pointer to memory location) which will explain later.
String type is actually a string of characters declare inside of double quotation marks " " or single quotation marks ' ' . Everything inside the marks computer will understand as string value.
Boolean type is either true or false represents to an on or off switches in game. That because computer has only two states 1 and 0. 1 is on and 0 is off. In programming language, these two states represent by boolean true or false . It is important to understand this concept because most of programming instructions written for computer return one of these two values.
Number type is of math numbers (integers, float, decimal...etc.) and to differentiate with string value it is written without marks. So the computer can differentiate between string of for example "34" and math number of 34.
Null type is empty value preserve for another value to be assigned to that variable. When you declare variable with null type, you basically tell the computer preserve that memory location for your variable however you have not decided to assign any computation value into that variable yet.
Undefined type is similar to writing instruction but you don't tell computer preserve any memory location for it. The computer don't know the existence of that value. So whenever you get "undefined" from computer means computer don't know anything about it.