JAVASCRIPT
THE BASICS
Created by /Bruno Paulino @brunojppb
BASIC TYPES
Number
String
Boolean
Object
Function
Undefined
VARIABLE DECLARATION
var num = 10; //number
var str = "hello, there"; //string
var bool = true; //boolean
var foo = { //object
name: 'Bar'
};
var bark = function() { //function
console.log('Ruff!');
};
OPERATIONS
Unary
console.log(typeof 23.0);
// number
Binary
if(102 > 101) {
console.log('greater than');
}
// greater than
Ternary
var discount = 10 > 12 ? "10% OFF" : "20% OFF";
console.log("Code: " + discount);
// Code: 20% OFF
OPERATORS
Logical
|| && == != === !===
Arithmetic
* / - + %
"==" AND "===" ARE NOT THE SAME
if("5" == 5) {
console.log("Same thing.");
}
// Same Thing.
if("5" === 5) {
console.log("same thing.");
} else {
console.log("Ops! This is not the same thing.");
}
// Ops! They are not the same thing.
WHY?
Data coercion
“Javascript will quietly convert that value
to the type it wants, using a set of rules
that often aren’t what you want or expect.”
Eloquent Javasctipt
SHORT-CIRCUITING
console.log(null || 'Foo');
// Foo
console.log('Foo' || 'Bar');
// Foo
console.log('Bruno' && 0);
// 0
console.log(false && 0);
// false
LOOP
for(var i = 0; i < 5; i++) {
console.log("i: " + i);
}
// 5 times: "i: n"
var counter = 0;
while(counter < 5) {
console.log('counter: ' + counter);
counter++;
}
// 5 times: "counter: n"
var newCounter = 0;
do {
console.log('newCounter: ' + newCounter);
newCounter++;
}while(newCounter < 5);
// 5 times: "counter: n"
FUNCTIONS
function sayHello() {
console.log('Hello!');
}
var sayHiTo = function(name) {
console.log('Hi ' + name);
}
sayHello();
sayHiTo("Bruno");
OPTIONAL ARGUMENTS
var sayHello = function(name) {
if (name == undefined)
console.log('Hello! There!');
else
console.log('Hello! ' + name + "!");
}
sayHello();
// Hello! There!
sayHello("bruno");
// Hello! Bruno!
CHALLENGE 1
Write a function that calculates the power of a number. The
first argument is required(the base) and the second should
be optional(the exponent), which is 2 by default.
Solution
CHALLENGE 2
Write a function that creates a chess grid dinamically with a
space and a hashtag(#). The function should receive 2
arguments. The first one is the width, the second one is the
height of the grid. The output should look like this for a 8x4
chess grid:
// call function
createChess(8, 4);
//output:
/*
# # # #
# # # #
# # # #
# # # #
*/
Solution
THANK YOU
REFERENCES:
Eloquent Javascript
Professional Javascript for web developers

Javascript - The basics

  • 1.
    JAVASCRIPT THE BASICS Created by/Bruno Paulino @brunojppb
  • 2.
  • 3.
    VARIABLE DECLARATION var num= 10; //number var str = "hello, there"; //string var bool = true; //boolean var foo = { //object name: 'Bar' }; var bark = function() { //function console.log('Ruff!'); };
  • 4.
    OPERATIONS Unary console.log(typeof 23.0); // number Binary if(102> 101) { console.log('greater than'); } // greater than Ternary var discount = 10 > 12 ? "10% OFF" : "20% OFF"; console.log("Code: " + discount); // Code: 20% OFF
  • 5.
    OPERATORS Logical || && ==!= === !=== Arithmetic * / - + %
  • 6.
    "==" AND "==="ARE NOT THE SAME if("5" == 5) { console.log("Same thing."); } // Same Thing. if("5" === 5) { console.log("same thing."); } else { console.log("Ops! This is not the same thing."); } // Ops! They are not the same thing. WHY? Data coercion “Javascript will quietly convert that value to the type it wants, using a set of rules that often aren’t what you want or expect.” Eloquent Javasctipt
  • 7.
    SHORT-CIRCUITING console.log(null || 'Foo'); //Foo console.log('Foo' || 'Bar'); // Foo console.log('Bruno' && 0); // 0 console.log(false && 0); // false
  • 8.
    LOOP for(var i =0; i < 5; i++) { console.log("i: " + i); } // 5 times: "i: n" var counter = 0; while(counter < 5) { console.log('counter: ' + counter); counter++; } // 5 times: "counter: n" var newCounter = 0; do { console.log('newCounter: ' + newCounter); newCounter++; }while(newCounter < 5); // 5 times: "counter: n"
  • 9.
    FUNCTIONS function sayHello() { console.log('Hello!'); } varsayHiTo = function(name) { console.log('Hi ' + name); } sayHello(); sayHiTo("Bruno");
  • 10.
    OPTIONAL ARGUMENTS var sayHello= function(name) { if (name == undefined) console.log('Hello! There!'); else console.log('Hello! ' + name + "!"); } sayHello(); // Hello! There! sayHello("bruno"); // Hello! Bruno!
  • 11.
    CHALLENGE 1 Write afunction that calculates the power of a number. The first argument is required(the base) and the second should be optional(the exponent), which is 2 by default. Solution
  • 12.
    CHALLENGE 2 Write afunction that creates a chess grid dinamically with a space and a hashtag(#). The function should receive 2 arguments. The first one is the width, the second one is the height of the grid. The output should look like this for a 8x4 chess grid: // call function createChess(8, 4); //output: /* # # # # # # # # # # # # # # # # */ Solution
  • 13.