Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: Java Script
Description: with this course you'lllearn the Java Script basics

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


Learn Javascript

Table of Contents
1
...
Basics
i
...
Variables
iii
...
Equality
3
...
Creation
ii
...
Advanced Operators
4
...
Creation
ii
...
Length
5
...
If
ii
...
Comparators
iv
...
Arrays
i
...
Length
7
...
For
ii
...
Do
...
Functions
i
...
Higher order
9
...
Creation
ii
...
Mutable
iv
...
Prototype
vi
...
Enumeration
viii
...
Whether you are an experienced programmer or not,
this book is intended for everyone who wishes to learn the JavaScript programming language
...
It was created in 1995, and is today one of the most famous and used programming languages
...

Programming means writing code
...
For now, the most
important is a statement
...
On its own, it has structure and purpose, but
without the context of the other statements around it, it isn't that meaningful
...
That's because statements tend to be written on
individual lines
...
You might be wondering what code (also called
source code) is
...

Therefore, a line of code is simply a line of your program
...


Basics

4

Learn Javascript

Comments
Comments are statements that will not be executed by the interpreter, comments are used to mark annotations for other
programmers or small descriptions of what your code does, thus making it easier for others to understand what your code
does
...
If you remember it from school, algebra
starts with writing terms such as the following
...
In programming, variables are containers for values that change
...
Variables have a name and a value separated by an equals
sign (=)
...

Let's check out how it works in Javascript, The following code defines two variables, computes the result of adding the two
and defines this result as a value of a third variable
...
This is where variable types
come in
...

The most common types are:
Numbers
Float: a number, like 1
...
5, 100004 or 0
...
233
String: a line of text like "boat", "elephant" or "damn, you are tall!"
Boolean: either true or false, but nothing else
Arrays: a collection of values like: 1,2,3,4,'I am bored now'
Objects: a representation of a more complex object
null: a variable that contains null contains no valid Number, String, Boolean, Array, or Object
undefined: the undefined value is obtained when you use an object property that does not exist, or a variable that has
been declared, but has no value assigned to it
...
You just need to use the var keyword to indicate that you are declaring a variable, and the interpreter will
work out what data type you are using from the context, and use of quotes
...


Types

7

Learn Javascript

Equality
Programmers frequently need to determine the equality of variables in relation to other variables
...

The most basic equality operator is the == operator
...

For example, assume:

var foo = 42;
var bar = 42;
var baz = "42";
var qux = "life";


foo == bar
will evaluate to true and baz == qux will evaluate to false , as one would expect
...
Behind the scenes the == equality operator attempts to
force its operands to the same type before determining their equality
...

The === equality operator determines that two variables are equal if they are of the same type and have the same value
...
baz === qux will still evaluate to false
...
It's the same as Java's double
...
0 are the same value
...


Numbers

9

Learn Javascript

Creation
Creating a number is easy, it can be done just like for any other variable type using the var keyword
...
2;
// This is an integer:
var b = 10;

Or from the value of another variable:

var a = 2;
var b = a;

Exercise
Create a variable `x` which equals `10` and create a variable `y` which equals `a`
...

var a = 2034547;
var b = 1
...
768;
var d = 45084;
var x =

Basic Operators

11

Learn Javascript

Advanced Operators
Some advanced operators can be used, such as:
Modulus (division remainder): x = y % 2
Increment: Given a = 5

c = a++
, Results: c = 5 and a = 6

c = ++a
, Results: c = 6 and a = 6

Decrement: Given a = 5

c = a--
, Results: c = 5 and a = 4

c = --a
, Results: c = 4 and a = 4

Exercise
Define a variable `c` as the modulus of the decremented value of `x` by 3
...
They represent text
based messages and data
...
How to create new strings and perform common operations on them
...


Exercise
Create a variable named `str` set to the value `"abc"`
...
This is done in JavaScript using the + operator
...

var firstName = "John";
var lastName = "Smith";
var fullName =

Concatenation

15

Learn Javascript

Length
It's easy in Javascript to know how many characters are in string using the property
...


// Just use the property
...
length;

Note: Strings can not be substracted, multiplied or divided
...

var str = "Hello World";
var size =

Length

16

Learn Javascript

Conditional Logic
A condition is a test for something
...
If you blindly trust data, you’ll get into trouble and your programs will fail
...
Taking such precautions is also known as programming defensively
...
You might have encountered branching diagrams before,
for example when filling out a form
...

In this chapter, we'll learn the base of conditional logic in Javascript
...
The condition has to be true for the
code inside the curly braces to be executed
...


Exercise
Fill up the value of `name` to validate the condition
...
This is very powerful if you want to react
to any value, but single out one in particular for special treatment:

var umbrellaMandatory;
if(country === 'England'){
umbrellaMandatory = true;
} else {
umbrellaMandatory = false;
}

The else clause can be joined with another if
...

} else if(country === 'France') {

...

}

Exercise
Fill up the value of `name` to validate the `else` condition
...

}

The conditional part is the variable country followed by the three equal signs ( === )
...
You can test conditions with double equal

signs, too, however a conditional such as if (x == 5) would then return true for both var x = 5; and var x =
"5";

...
It is highly recommended as

a best practice that you always compare equality with three equal signs ( === and !== ) instead of two ( == and != )
...

var x = 6;
var a = 0;

Logical Comparison
In order to avoid the if-else hassle, simple logical comparisons can be utilised
...
The code says that if the value of marks is greater than 85 i
...
marks >
85
, then topper = YES ; otherwise topper = NO
...


Comparators

20

Learn Javascript

Concatenate conditions
Furthermore you can concatenate different conditions with "or” or “and” statements, to test whether either statement is true,
or both are true, respectively
...

Say you want to test if the value of x is between 10 and 20—you could do that with a condition stating:

if(x > 10 && x < 20) {

...

}

Note: Just like operations on numbers, Condtions can be grouped using parenthesis, ex: if ( (name === "John"
|| name === "Jennifer") && country === "France")

...
An array is a list of data
...
It also makes it much easier to perform functions on related data
...

Here is a simple array:

// 1, 1, 2, 3, 5, and 8 are the elements in this array
var numbers = [1, 1, 2, 3, 5, 8];

Arrays

22

Learn Javascript

Indices
So you have your array of data elements, but what if you want to access a specific element? That is where indices come in
...
indices logically progress one by one, but it should be noted that the first index in an
array is 0, as it is in most languages
...


// This is an array of strings
var fruits = ["apple", "banana", "pineapple", "strawberry"];
// We set the variable banana to the value of the second element of
// the fruits array
...
Result: banana = "banana"
var banana = fruits[1];

Exercise
Define the variables using the indices of the array
var cars = ["Mazda", "Honda", "Chevy", "Ford"]
var honda =
var ford =
var chevy =
var mazda =

Indices

23

Learn Javascript

Length
Arrays have a property called length, and it's pretty much exactly as it sounds, it's the length of the array
...
length;

Exercise
Define the variable a to be the number value of the length of the array
var array = [1, 1, 2, 3, 5, 8];
var l = array
...
Loops are handy, if you want to run the same code
over and over again, each time with a different value
...
length; i++) {
doThing(cars[i]);
}

Loops

25

Learn Javascript

For Loop
The easiest form of a loop is the for statement
...


Exercise
Using a for-loop, create a variable named `message` that equals the concatenation of integers (0, 1, 2,
...

var message = "";

For

26

Learn Javascript

While Loop
While Loops repetitively execute a block of code as long as a specified condition is true
...
This loop will execute the code block once before checking if the condition
is true
...
) as long as
its length (`message
...

var message = "";

While

27

Learn Javascript

Do
...
while statement creates a loop that executes a specified statement until the test condition evaluates to be false
...
Syntax for do
...
while loop:

var i = 0;
do {
document
...


Exercise
Using a do
...

var i = 0;

Do
...

Functions like mathematical functions perform transformations, they take input values called arguments and return an
output value
...
Let's declare a function double that accepts an argument called x and
returns the double of x :

function double(x) {
return 2 * x;
}

Note: the function above may be referenced before it has been defined
...
) and given to
other functions as arguments :

var double = function(x) {
return 2 * x;
};

Note: the function above may not be referenced before it is defined, just like any other variable
...


Declare

30

Learn Javascript

Higher Order Functions
Higher order functions are functions that manipulate other functions
...
Such fancy functional techniques are powerful constructs available
to you in JavaScript and other high-level languages like python, lisp, etc
...
map will accept two
arguments, func and list (its declaration will therefore begin map(func,list) ), and return an array
...


// Define two simple functions
var add_2 = function(x) {
return x + 2;
};
var double = function(x) {
return 2 * x;
};
// map is cool function that accepts 2 arguments:
// func the function to call
// list a array of values to call func on
var map = function(func, list) {
var output=[]; // output list
for(idx in list) {
output
...
However, when passed as arguments to other functions, they can be
composed in unforeseen ways to build more complex functions
...
) and map(double,
...
Using function composition, we could do this as follows:

process_add_2 = function(list) {
return map(add_2, list);
}
process_double = function(list) {
return map(double, list);
}
process_add_2([5,6,7]) // => [7, 8, 9]
process_double([5,6,7]) // => [10, 12, 14]

Now let's create a function called buildProcessor that takes a function func as input and returns a func -processor, that is,
a function that applies func to each input in list
...
We'll create a function called buildMultiplier that takes a number x as input and returns a
function that multiplies its argument by x :

var buildMultiplier = function(x) {
return function(y) {
return x * y;
}
}
var double = buildMultiplier(2);
var triple = buildMultiplier(3);
double(3); // => 6
triple(3); // => 9

Exercise
Define a function named `negate` that takes `add1` as argument and returns a function, that returns the negation of the
value returned by `add1`
...
Every other value is an

object

...


Objects

33

Learn Javascript

Creation
There are two ways to create an object in JavaScript:
1
...

2
...


Creation

34

Learn Javascript

Properties
Object's property is a propertyName : propertyValue pair, where property name can be only a string
...
You can specify properties when creating an object or later
...


var language = {
name: 'JavaScript',
isSupportedByBrowsers: true,
createdIn: 1995,
author:{
firstName: 'Brendan',
lastName: 'Eich'
},
// Yes, objects can be nested!
getAuthorFullName: function(){
return this
...
firstName + " " + this
...
lastName;
}
// Yes, functions can be values too!
};

The following code demonstates how to get a property's value
...
name;
// variable now contains "JavaScript" string
...
The difference is that the second one lets you use litteraly any string as a property name, but
variable = language
...


The following example shows how to add a new property or change an existing one
...
newProperty = 'new value';
// Now the object has a new property
...

language['newProperty'] = 'changed value';
// Once again, you can access properties both ways
...


Properties

35

Learn Javascript

Mutable
The difference between objects and primitive values is that we can change objects, whereas primitive values are
immutable
...

var myObject = { key: "first value"};
myObject
...


Mutable

36

Learn Javascript

Reference
Objects are never copied
...


// Imagine I had a pizza
var myPizza = {slices: 5};
// And I shared it with You
var yourPizza = myPizza;
// I eat another slice
myPizza
...
slices - 1;
var numberOfSlicesLeft = yourPizza
...

var a = {}, b = {}, c = {};
// a, b, and c each refer to a
// different empty object
a = b = c = {};
// a, b, and c all refer to
// the same empty object

Reference

37

Learn Javascript

Prototype
Every object is linked to a prototype object from which it inherits properties
...
prototype, which is an object that comes
standard with JavaScript
...
age;
// The line above

First, the interpreter looks through every property the object itself has
...
But besides that one it actually has a few more properties, which were inherited from Object
...


var stringRepresentation = adult
...
prototype's property, which was inherited
...
If you want it to return a more meaningful representation, then you can override it
...


adult
...
age;
}

If you call the toString function now, the interpreter will find the new property in the object itself and stop
...

To set your own object as a prototype instead of the default Object
...
create as follows:

var child = Object
...
prototype with the one we want
...
age = 8;
/* Previously, child didn't have its own age property, and the interpreter had to look further to the child's prototype to find it
...

Note: adult's age is still 26
...
toString();
// The value is "I'm 8"
...
If adult did not have toString


child
's prototype is adult , whose prototype is Object
...
This sequence of prototypes is called prototype chain
...
It will remove a property from the object if it has one
...
Removing a property from an object may allow a property from the prototype chain to
shine through:

var adult = {age:26},
child = Object
...
age = 8;
delete child
...
*/
var prototypeAge = child
...


Delete

39

Learn Javascript

Enumeration
The for in statement can loop over all of the property names in an object
...


var fruit = {
apple: 2,
orange:5,
pear:1
},
sentence = 'I have ',
quantity;
for (kind in fruit){
quantity = fruit[kind];
sentence += quantity+' '+kind+
(quantity===1?'':'s')+
', ';
}
// The following line removes the trailing coma
...
substr(0,sentence
...
';
// I have 2 apples, 5 oranges, 1 pear
...

Suppose we are developing a counter module:

var myCounter = {
number : 0,
plusPlus : function(){
this
...
number + 1;
},
isGreaterThanTen : function(){
return this
...

The module now takes only one variable name — myCounter
Title: Java Script
Description: with this course you'lllearn the Java Script basics