The basics of java-script

What does Java-script do?

JavaScript allows you to make interactive web pages by accessing and modifying the content and markup used in a web page while being viewed in a browser. One rule with java script is that you must use camelCase while naming variables, objects and instances.

Some history

Javascript has nothing to do with the programming language ‘Java’. In 1995 when javascipt was introduced by Netscape, the java language was being heavily marketed and gaining popularity hence the name was made for marketing purposes.

Values, variables

There is only data in the computer world. All data in essence is just a sequence of bits and thus every piece of data has it’s own role. In javascripts system most of the data is neatly separated into things called values. There are seven basic types of values including numbers, strings, booleans, objects, functions, null and undefined values.

Variables allow us to store data in a dynamic fashion by using “label” to point to the data rather than using the data itself. All data types can be stored in a variable.

In javascript we declare a value by using the world in front of it as shown below

var ourName;

Here we just named the variable ourName, now if we want to store data in it we use an assignment operator which is =

var ourName = 6;

We have store the number 6 inside the ourName variable. It is now holding data.

Operators

Operators that take two values are binary operators whilst those that take one are unary operators. The minus operator can be both unary and binary.

-(10-2)

Boolean types use true or false

3 > 2   3 < 2

Here is a list of the following operators that are used a lot in javascript.

>= is greater than or equal to 
<= is less than or equal to
== is equal to
!= is not equal to
=== equal value and type example: x === 5 will be false
&& AND, eg: x<10&&x!==5 will print false. This logical operator require both to be true or false
||  OR, this logical operator takes both sides but requires one to be true
! NOT, this operator flips the right one to the opposite answer, so if it's true and we add ! at the front it automatically becomes false. For example !(x===y) will be equal to true because we added ! at the front event though it is false without it.

Javascript statements and conditionals

Statements in javascript are instructions and each one starts on a new line and end with a semi colon. The semi colon also tells the javascript interpreter when a step is over so that it can move to the next step. Some statements are surrounded by curly braces known as code blocks.

Conditionals are how you add logic to your code. The three main conditionals include if, else if and else. Below is a javascript time greeter that i have created using conditionals. Anything written in // is a comment.

// we have assigned new date value to the variable 'today'. in the next variable we have assigned the today which has the new date for the day with get hours to the hourNow variable. This will print out the current hour of the current date. 
// for the last variable we have left it as is to be able to assign it different values where the statement begins.
var today = new Date();
var hourNow = today.getHours();
var greeting;
// if the time is more than 18 hours print out the greeting, else if it is more than 12 hours print out greeting, else more than 0 print out the greeting. Each statement has it's own greeting assigned
if(hourNow > 18) {
greeting = 'Good evening!';
} else if (hourNow > 12) {
greeting = 'good Afternoon!';
} else if (hourNow > 0) {
greeting = 'good morning!';
} else {
greeting = 'welcome!';
}
// here we have called the method
// the document object represents the entire web page , all web browsers implement this object
// the dot next to is called a member operator and it allows us to access members of an object by adding it
// the write() method allows content to be written in our page where script element sits in our html file
// data is written inside paramteres so that the method knows that to do ()
document.write('<h3>' + greeting + '</h3>');

Where there are parameters (), that is where data is stored so that the method knows what to do. Empty parameters are sometimes used when adding values to variables to be able to add data in the statement. Statements should end with a semi colon ; .

Look forward to part 2 :)