Variables are declared without a type, using the keyword 'var', and their type is determined internally
There is no type checking like in strong typed languages
var a = 3; //number
var b = '4'; //string
var c = true; //boolean
var arr = []; //array
var obj = {}; //object
//Invalid declarations:
number a = 3;
string b ='4';
string
number
boolean
Object
function
Array
Date
RegEx
(primitive types)
There are a few simple JavaScript Types:
(types of objects)
null
undefined
(special cases)
function add(a,b){
return a+b;
}
c = add(1,2);
console.log(c); //prints 3
function statements
function add(a,b){
return a+b;
}
c = add(1,2);
console.log(c); //prints 3
function statements
var add = function(a,b){
return a+b;
}
c = add(1,2);
console.log(c); //prints 3
function expressions
function add(a,b){
return a+b;
}
c = add(1,2);
console.log(c); //prints 3
function statements
var add = function(a,b){
return a+b;
}
c = add(1,2);
console.log(c); //prints 3
function expressions
function add(a,b){
return a+b;
}
function calculate(passfunction,a,b){
return passfunction(a,b)
}
c = calculate(add,1,2);
console.log(c); //prints 3
pass them to other functions
function add(a,b){
return a+b;
}
c = add(1,2);
console.log(c); //prints 3
function statements
var add = function(a,b){
return a+b;
}
c = add(1,2);
console.log(c); //prints 3
function expressions
function add(a,b){
return a+b;
}
function calculate(passfunction,a,b){
return passfunction(a,b)
}
c = calculate(add,1,2);
console.log(c); //prints 3
pass them to other functions
var joe = {
say :function(){
console.log("bang bang");
}
}
joe.say();
as methods
function Actor(name) {
this.name = name;
}
Actor.prototype.speak = function(sentence) {
console.log(this.name + ' said ' + sentence);
}
var angelEyes = new Actor('Angel eyes');
angelEyes.speak('this is just the begining...');
var joe = new Actor('Joe');
joe.speak('of what?');
var tuco = new Actor('Tuco');
tuco.speak('of the desert.');
var dog = {
name:'dog with no name',
food: ['wind','sand','gringos'],
get Name() {
return this.name;
},
set Name(value) {
//I already have a name
}
};
dog.toString = function(){
return this.Name +' eats: '+ this.food.join(',');
};
dog.breed = 'mexican';
dog.Name = 'Chili peppers'
console.log(dog.toString())
//dog with no name eats: wind,sand,gringos
var empty = {};
console.log(empty.toString) //Function: toString()
console.log(empty.toString()) //[Object, Object]
var empty = {};
console.log(empty.toString) //Function: toString()
console.log(empty.toString()) //[Object, Object]
Object.getPrototypeOf(Object.prototype) //null
Object.getPrototypeOf({}) == Object.prototype //true
Object.getPrototypeOf(isNaN) == Function.prototype //true
Object.getPrototypeOf([]) == Array.prototype //true
var protoRabbit = {
eat: function(food) {
console.log(this.type + ' rabbit eats ' + food)
}
}
var romanianRabbit = Object.create(protoRabbit);
romanianRabbit.type = 'romanian';
romanianRabbit.eat('carrots');
var mexicanRabbit = Object.create(protoRabbit);
mexicanRabbit.type = 'mexican';
mexicanRabbit.eat('beans of course');
//romanian rabbit eats carrots
//mexican rabbit eats beans of course
function Actor(name) {
this.name = name;
}
Actor.prototype.speak = function(sentence) {
console.log(this.name + ' said ' + sentence);
}
var angelEyes = new Actor('Angel eyes');
angelEyes.speak('this is just the begining...');
var joe = new Actor('Joe');
joe.speak('of what?');
var tuco = new Actor('Tuco');
tuco.speak('of the desert.');
// Angel eyes: this is just the begining...
// Joe: of what?
// Tuco: of the desert.
function Human(name) {
this.name = name;
}
Human.prototype.speak = function(sentence) {
console.log(this.name + ' said ' + sentence);
}
function Human(name) {
this.name = name;
}
Human.prototype.speak = function(sentence) {
console.log(this.name + ' said ' + sentence);
}
function Actor(name) {
Human.call(this, name);
}
Actor.prototype = Object.create(Human.prototype);
Actor.prototype.speak = function(sentence) {
console.log('where are my money?')
}
Actor.prototype.play = function(movie) {
console.log(this.name + ' played in movie ' + movie);
}
function Human(name) {
this.name = name;
}
Human.prototype.speak = function(sentence) {
console.log(this.name + ' said ' + sentence);
}
function Actor(name) {
Human.call(this, name);
}
Actor.prototype = Object.create(Human.prototype);
Actor.prototype.speak = function(sentence) {
console.log('where are my money?')
}
Actor.prototype.play = function(movie) {
console.log(this.name + ' played in movie ' + movie);
}
var actor = new Actor('Clint Eastwood');
actor.speak('hello everybody');
actor.play('the good, the bad & the ugly');
//where are my money?
//Clint Eastwood played in movie the good, the bad & the ugly
var x = eval('3+2');
console.log(typeof x); //number
var x = eval('3+2');
console.log(typeof x); //number
var x = eval('3+2');
console.log(typeof x); //number
var x = eval('3+2');
console.log(typeof x); //number
function getBlackBox() {
eval('x=123');
eval('add = function(a,b) {return a + b;};')
console.log(add(1, 5)); //6
console.log(x) //123
}
var x = eval('3+2');
console.log(typeof x); //number
function getBlackBox() {
eval('x=123');
eval('add = function(a,b) {return a + b;};')
console.log(add(1, 5)); //6
console.log(x) //123
}
function getBlackBox() {
'use strict' //ECMAScript 5 strict mode
eval('x=123'); //ReferenceError: x is not defined
eval('add = function(a,b) {return a + b;};')
}
evaluated code can query and set local variables, but cannot define new variables or functions in the local scope.
function getBlackBox() {
'use strict' //ECMAScript 5 strict mode
eval('x=123'); //ReferenceError: x is not defined
eval('add = function(a,b) {return a + b;};')
}
evaluated code can query and set local variables, but cannot define new variables or functions in the local scope.
function getBlackBox() {
'use strict' //ECMAScript 5 strict mode
var x, add;
eval('x=123');
eval('add = function(a,b) {return a + b;};')
console.log(x, add(10, 20)); //123, 30
}
var a = new Number(10);
var b = new Boolean(false);
var c = new String('Go with the flow, my friend!');
if (b) {
console.log('this is my lucky day!')
}
console.log(a,b,c);
console.log(typeof a, typeof b, typeof c);
var a = new Number(10);
var b = new Boolean(false);
var c = new String('Go with the flow, my friend!');
if (b) {
console.log('this is my lucky day!')
}
console.log(a,b,c);
console.log(typeof a, typeof b, typeof c);
//this is my lucky day
//[Number: 10] [Boolean: false] [String: 'Go with the flow, my friend!']
//object object object
var a = new Number(10);
var b = new Boolean(false);
var c = new String('Go with the flow, my friend!');
if (b == true) {
console.log('this is my lucky day!')
}
console.log(a.valueOf(), b.valueOf(), c.valueOf());
console.log(typeof a.valueOf(), typeof b.valueOf(), typeof c.valueOf());
//10 false 'Go with the flow, my friend!'
//[Number] [Boolean] [String]
function getPerson() {
return
{
name: 'Bill'
}
}
a = getPerson()
function getPerson() {
return
{
name: 'Bill'
}
}
a = getPerson()
function getPerson() {
return;
{
name: 'Bill'
}
}
a = getPerson() //undefined
function getPerson() {
return
{
name: 'Bill'
}
}
a = getPerson()
function getPerson() {
return;
{
name: 'Bill'
}
}
a = getPerson() //undefined
function getPerson() {
return {
name: 'Bill'
}
}
a = getPerson() // { name: 'Bill' }
'' == '0'
'' == '0' // false
'' == '0' //false
0 == ''
'' == '0' //false
0 == '' //true
'' == '0' //false
0 == '' //true
'' == '0' //false
0 == '' //true
0 == '0'
'' == '0' //false
0 == '' //true
0 == '0' //true
'' == '0' //false
0 == '' //true
0 == '0' //true
false == 'false'
'' == '0' //false
0 == '' //true
0 == '0' //true
false == 'false' //false
'' == '0' //false
0 == '' //true
0 == '0' //true
false == 'false' //false
false == '0'
'' == '0' //false
0 == '' //true
0 == '0' //true
false == 'false' //false
false == '0' //true
'' == '0' //false
0 == '' //true
0 == '0' //true
false == 'false' //false
false == '0' //true
null == undefined
'' == '0' //false
0 == '' //true
0 == '0' //true
false == 'false' //false
false == '0' //true
null == undefined //true
'' == '0' //false
0 == '' //true
0 == '0' //true
false == 'false' //false
false == '0' //true
null == undefined //true
var x = 5
if (true) {
var y = 10
}
console.log(x + y)
var x = 5
if (true) {
var y = 10
}
console.log(x + y) //15
var x = 5
if (true) {
var y = 10
}
console.log(x + y) //15
var x = 5
function test() {
var y = 10
}
console.log(x + y)
var x = 5
if (true) {
var y = 10
}
console.log(x + y) //15
var x = 5
function test() {
var y = 10
}
console.log(x + y) //y is not defined
var x = 5
if (true) {
var y = 10
}
console.log(x + y) //15
var x = 5
function test() {
var y = 10
}
console.log(x + y) //y is not defined
let x = 5
if (true) {
let y = 10
}
console.log(x + y) //y is not defined
//Global Scope
var a = 1; //window.a = 1
function scopeTest() {
console.log(a);
}
scopeTest(); //1
//Global Scope
var a = 1; //window.a = 1
function scopeTest() {
console.log(a);
}
scopeTest(); //1
//Global Scope
var a = 1;
function scopeTest() {
a = 2; //Overwrites global variable 2, you omit 'var'
console.log(a);
}
console.log(a); //1
scopeTest(); //2
console.log(a); //2 (global value is overwritten)
//Global Scope
var a = 1; //window.a = 1
function scopeTest() {
console.log(a);
}
scopeTest(); //1
//Global Scope
var a = 1;
function scopeTest() {
a = 2; //Overwrites global variable 2, you omit 'var'
console.log(a);
}
console.log(a); //1
scopeTest(); //2
console.log(a); //2 (global value is overwritten)
//Global Scope
var a = 1;
function scopeTest() {
var a = 2;
console.log(a);
}
console.log(a); //1
scopeTest(); //2
console.log(a); //1
function Actor(name) {
this.name = name
}
Actor.prototype.logMovies = function(movies) {
return movies.map(function(movie) {
console.log(this.name + ' played in the movie ' + movie)
})
}
var actor = new Actor('Clint Eastwood')
actor.logMovies(['The Good, the Bad and the Ugly', 'Invictus'])
function Actor(name) {
this.name = name
}
Actor.prototype.logMovies = function(movies) {
return movies.map(function(movie) {
console.log(this.name + ' played in the movie ' + movie)
})
}
var actor = new Actor('Clint Eastwood')
actor.logMovies(['The Good, the Bad and the Ugly', 'Invictus'])
/* */
undefined played in the movie The Good, the Bad and the Ugly
undefined played in the movie Invictus
function Actor(name) {
this.name = name
}
Actor.prototype.logMovies = function(movies) {
var self = this;
return movies.map(function(movie) {
console.log(self.name + ' played in the movie ' + movie)
})
}
var actor = new Actor('Clint Eastwood')
actor.logMovies(['The Good, the Bad and the Ugly', 'Invictus'])
/* */
Clint Eastwood played in the movie The Good, the Bad and the Ugly
Clint Eastwood played in the movie Invictus
function Actor(name) {
this.name = name
}
Actor.prototype.logMovies = function(movies) {
return movies.map(function(movie) {
console.log(this.name + ' played in the movie ' + movie)
}, this)
}
var actor = new Actor('Clint Eastwood')
actor.logMovies(['The Good, the Bad and the Ugly', 'Invictus'])
/* */
Clint Eastwood played in the movie The Good, the Bad and the Ugly
Clint Eastwood played in the movie Invictus
function Actor(name) {
this.name = name
}
Actor.prototype.logMovies = function(movies) {
return movies.map(function(movie) {
console.log(this.name + ' played in the movie ' + movie)
}.bind(this))
}
var actor = new Actor('Clint Eastwood')
actor.logMovies(['The Good, the Bad and the Ugly', 'Invictus'])
/* */
Clint Eastwood played in the movie The Good, the Bad and the Ugly
Clint Eastwood played in the movie Invictus
function Actor(name) {
this.name = name
}
Actor.prototype.logMovies = function(movies) {
return movies.map(movie =>
console.log(this.name + ' played in the movie ' + movie)
)
}
var actor = new Actor('Clint Eastwood')
actor.logMovies(['The Good, the Bad and the Ugly', 'Invictus'])
/* */
Clint Eastwood played in the movie The Good, the Bad and the Ugly
Clint Eastwood played in the movie Invictus
x = 5; // Assign 5 to x
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x; // Display x in the element
var x; // Declare x
var x; // Declare x
x = 5; // Assign 5 to x
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x; // Display x in the element
var x = 5; // Initialize x
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x + " " + y; // Display x and y
var y = 7; // Initialize y
var x = 5; // Initialize x
var y = 7; // Initialize y
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x + " " + y; // Display x and y