Wednesday, May 28, 2014

JavaScript - Chapter 1 - Basics

JavaScript - Chapter 1 - Basics

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.


There are five primitive data types in JavaScript and they are as follows:

There are other types that are objects, arrays, and functions. Objects are used to hold data as properties as well as perform tasks via defined functions when they are attached to an Object most refer to them as methods. Arrays also hold data but that is their main purpose. Function executes a snippet of logic takes parameters and usually returns values.  Here are a few examples of the more complex types as follows:


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. 


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.