CodePamoja
CodePamoja means "Code Together" in Swahili. We are a group of passionate developers in The Netherlands and Kenya working together in virtual teams, transferring the latest software development skills to each other.
by Danny, Joep & Tino
// an empty variable, also called undefined
var foo;
// variable with a string
var foo = 'This is a string';
// variable with a number
var foo = 10;
// boolean
var foo = false;
// Bad
var averylongname;
var p1;
// Good
var aVeryLongName;
var person1;
// using the + to combine strings
var firstName = 'John';
var lastName = 'Doe';
var fullName = firstName + ' ' + lastName;
// using the concat() method
var firstName = 'John ';
var lastName = 'Doe';
var fullName = firstName.concat(lastName);
// output will be:
John Doe
if (10 === '10') { // condition
console.log('foo'); // statement
} else { // condition
console.log('bar'); // statement
}
// Integers
10
3
-15
-989
0
// Scientific notation
5e-6 // Which is .000005
3e+6 // Which is 3000000
2e+2 // Which is 200
// Floating point numbers
4.56
-0.9342
1.5
.26
// Addition // Multiplication
10 + 5 10 * 5
// Subtraction // Division
10 - 5 10 / 5
var totalScore;
var playerScore1 = 10,
playerScore2 = 5,
playerScore3 = 6,
playerScore4 = 2;
// Addition
totalScore = playerScore1 + playerScore2;
console.log(totalScore);
// Subtraction
totalScore = playerScore4 - playerScore3;
console.log(totalScore);
// Multiplication
totalScore = playerScore3 * playerScore1;
console.log(totalScore);
// Division
totalScore = playerScore1 / playerScore4;
console.log(totalScore);
Used to perform arithmetic between variables and/or values
// addition: output = 15 // modulus: output = 2
10 + 5; 12 % 5;
// subtraction: output = 45 // incement
145 - 100; ++
// multiplication: output = 12 // decrement
2 * 6; --
// division: output = 3
12 / 4;
Used in logical statements to determine equality or difference between variables or values
== // equal to
=== // equal value and equal type (recommended)
!= // not equal
!== // not equal value or not equal type (recommended)
> // greater than
< // less than
>= // greater than or equal to
<= // less than or equal to
Used to determine the logic between variables or values
&& // and
|| // or
! // not
Assigns a value to a variable based on a condition
if (condition) {
action = 'do something';
}
else {
action = 'do something else';
}
// can be written as
action = (condition) ? 'do something' : 'do something else';
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Loops through the properties of an object
var person = {
firstName:"John",
lastName:"Doe",
age:25
};
var text = "";
var x;
for (x in person) {
text += person[x];
}
while (condition) {
// code block to be executed
}
while (i < 10) {
text += 'The number is ' + i;
i++;
}
do {
// code block to be executed
}
while (condition);
do {
text += 'The number is ' + i;
i++;
}
while (i < 10);
var arrayName = [];
var arrayName = ['name1', 'name2', 'name3'];
// If you have a long list
var arrayName = [
'name1',
'name2',
'name3',
'name4',
'name5',
'name6'
];
// Current Array
var arrayName = [22, 23, 24, 25, 26];
// Add data to Array
arrayName.push(27);
// Result
arrayName = [22, 23, 24, 25, 26, 27];
var developers = [
'Phil',
'Joe',
'Ryan',
'David',
'Carla'
];
for( var i = 0; i < developers.length; i += 1) {
console.log(developers[i]);
}
var developers = [
'Phil',
'Joe',
'Ryan'
];
var designers = [
'Jeroen',
'Kathy',
'Billy'
];
developers.concat(designers);
// Output would be:
[
'Phil',
'Joe',
'Ryan',
'Jeroen',
'Kathy',
'Billy'
];
Containers for named values
Each object can have:
// Object literal
var objectName = {};
var developer = {
name: 'John',
age: 30,
hobbies: ['Coding', 'Gaming']
};
Could call it "characteristics" of an object
It has a name and a value
var person = {
firstName: 'Klaas',
lastName: 'Jansen',
age: 30,
job: 'Software developer'
};
Could also be called "interaction"
When an event happens, can be used to trigger an action
An object property containing a function definition
Actions that can be performed on objects
var person = {
firstName: 'Klaas',
lastName: 'Jansen'
age: 30,
job: 'Software developer',
fullName: function() {
return this.firstName + ' ' + this.lastName;
}
};
Using the properties name as a string
Dot notation
var person = {
name: 'John Doe',
age: 30,
profession: 'developer',
hobbies: ['Coding', 'Gaming'],
single: true
};
// Option 1: Using the properties name as string
person['name']
person['age']
// Option 2: Dot notation (more common!)
person.name
person.age
Literal notation (individual object)
Constructor notation (multiple objects)
Properties an methods can be added to the object afterwards
var person = {}
person.firstName = 'Klaas';
person.lastName = 'Jansen';
person.age = '30';
person.job = 'Software developer';
person.fullName = function() {
return this.firstName + ' ' + this.lastName;
};
A colon separates the name (key) and values
A comma separates each pair
var person = {
firstName: 'Klaas',
lastName: 'Jansen'
age: 30,
job: 'Software developer',
fullName: function() {
return this.firstName + ' ' + this.lastName;
}
};
For storing data between applications
Takes less space in your code
For global objects that set up information for a page
The syntax for adding or removing properties and methods is the same
var person = new Object();
person.firstName = 'Klaas';
person.lastName = 'Jansen';
person.age = '30';
person.job = 'Software developer';
person.fullName = function() {
return this.firstName + ' ' + this.lastName;
};
A function can create multiple objects this keyword is being used in stead of the object name
function Person(firstName, lastName, age, job) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.job = job;
this.fullName = function() {
return this.firstName + ' ' + this.lastName;
};
}
// Creating a new instance with the "new" keyword
var developer1 = new Person('Klaas', 'Jansen', 30, 'Software developer');
var developer2 = new Person('Timo', 'de Jong', 18, 'Front-end developer');
A complex object might not be used in code
Object with similar used functionality within a page/application
var person = {
name: 'John Doe',
age: 30,
profession: 'developer',
hobbies: ['Coding', 'Gaming'],
single: true
};
person.name = 'Jane Doe';
person.age = 28;
person.email = 'jane.doe@mail.com';
console.log(person);
var person = {
name: 'John Doe',
age: 30,
profession: 'developer',
hobbies: ['Coding', 'Gaming'],
single: true
};
// Gives you access to the properties
for (var item in person) {
console.log(item);
}
// Give you full access to all data in Object
for (var item in person) {
console.log(item, ':', person[item]);
}
var developers = [
{
name: 'John',
age: 20,
skills: ['HTML', 'CSS', 'JavaScript']
},
{
name: 'Jane',
age: 25,
skills: ['Java', 'PHP', 'AngularJS']
}
];
// An array with an object
var developers = [
{
name: 'John',
age: 20,
skills: ['HTML', 'CSS', 'JavaScript']
},
{
name: 'Jane',
age: 25,
skills: ['Java', 'PHP', 'AngularJS']
}
];
// Looping through with a for loop
for( var i = 0; i < developers.length; i += 1) {
console.log(developers[i].name + ' is ' + developers[i].age + ' years old');
}
document
<html>
<head>
<body>
<title>
<link>
<h1>
<p>
<strong>
lorem ipsum
lorem ipsum
lorem ipsum
document
element
text
By accessing element nodes you can reach attribute and text nodes
It's possible to select an individual and multiple element nodes or moving an element node to a related node
// methods returning a single node
document.getElementById('id');
document.querySelector('css selector');
// methods returning a nodelist
document.getElementsByClassName('class');
document.getElementsByTagName('tagName');
document.querySelectorAll('css selector');
// creating a new element, a text node attach them
var newElement = document.createElement('<p>'),
newContent = document.createTextNode('Hi all');
newElement.appendChild(newContent);
// place them into its position
var positionElement = document.getElementsByTagName('div')[0];
positionElement.appendChild(newElement);
// getting value of an attribute
var foo = document.getAttribute();
// check if element has an attribute
var foo = document.hasAttribute();
// set value
var foo = document.setAttribute();
// remote attribute
var foo = document.removeAttribute();
// example: find an element and get value of attribute
var foo = document.getElementById('bar').getAttribute('class');
// retrieve or amend the content of a text node with nodeValue
var foo = document.getElementById('bar').firstChild.nodeValue = 'Hi all';
// collect or update text in the containing element with textContent
var foo = document.getElementById('bar').textContent = 'Hi all';
// access or amend content of an element incl. it's children
var foo = document.getElementById('bar').innerHTML = 'Hi all';
var size = 50 + 20;
document.write(size);
function size(height, width) {
var size = height + width;
return size;
}
By CodePamoja
Introduction to JavaScript
CodePamoja means "Code Together" in Swahili. We are a group of passionate developers in The Netherlands and Kenya working together in virtual teams, transferring the latest software development skills to each other.