// function statement
function greet (name) {
console.log('Hi ' + name + '!');
}
// function expression
var greet = function (name) {
console.log('Hi ' + name + '!');
};
greet('Bob'); // Hi Bob!
var test = function (condition) {
var num = 10;
if (condition) {
var num = 15;
console.log('inner num: ' + num);
}
console.log('outer num: ' + num);
};
test(false); // outer num: 10
test(true); // inner num: 15 | outer num: 15
var three = (function () {
return 3;
})();
console.log(three); // 3
var five = (function (x, y) {
return x + y;
})(2, 3);
console.log(five); // 5
var str = 'Hi, I am a global';
(function () {
var secret = 'P@s5w0rd';
})();
console.log(str); // Hi, I am a global
console.log(secret); // undefined
In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.
var sum = function (x, y) {
return x + y;
};
console.log(sum(2, 3)); // 5
var math = {
sum: sum,
mult: function (x, y) {
return x * y;
}
};
console.log(math.sum(2, 3)); // 5
console.log(math.mult(2, 5)); // 10
// Example 1
setTimeout(function () {
console.log('done!');
}, 3000);
// Example 2
var doIf = function (expr, task) {
if (expr) { task(); }
};
doIf(2 < 5, function () {
console.log('2 is less than 5');
});
// Example 3
var doAsync = function (done) {
connect(function () {
read(function (data) {
done(data);
});
});
};
doAsync(function (data) {
console.log(data);
});
// Example 4
var log = function (msg) {
console.log(msg);
};
doAsync(log);
var sum = function (a) {
return function (b) {
return a + b;
};
};
console.log(sum(3)(4)); // 7
var sum3 = sum(3);
console.log(sum3(4)); // 7
var counter = function () {
var count = 0;
return function () {
var curr = count;
count = count + 1;
return curr;
};
};
var tick = counter();
console.log(tick()); // 0
console.log(tick()); // 1
console.log(tick()); // 2
https://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript
var math = (function () {
var sum = function (a, b) {
return a + b;
};
var mult = function (a, b) {
return a * b;
};
var square = function (a) {
return mult(a, a);
};
return {
sum: sum,
mult: mult,
square: square
};
})();
console.log(math.square(3)); // 9
var numbers = [ 1, 2, 3, 4 ];
for (var i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
var numbers = [ 1, 2, 3, 4 ];
numbers.forEach(function (number) {
console.log(number);
});
var numbers = [ 1, 2, 3, 4 ];
var doubles = [];
for (var i = 0; i < numbers.length; i++) {
doubles.push(numbers[i] * 2);
}
var numbers = [ 1, 2, 3, 4 ];
var doubles = numbers.map(function (number) {
return number * 2;
});
var numbers = [ 1, 2, 3, 4 ];
var double = function () {
numbers.forEach(function (number, index) {
numbers[index] = number * 2
});
return numbers;
};
console.log(double()); // [ 2, 4, 6, 8 ]
console.log(numbers); // [ 2, 4, 6, 8 ]
Common side effects:
- Modifying any external variable or object property (e.g., a global variable, or a variable in the parent function scope chain)
- Logging to the console
- Writing to the screen
- Writing to a file
- Writing to the network
- Triggering any external process
- Calling any other functions with side-effects
var sum = function (a, b) {
return a + b;
};
var numbers = [ 1, 2, 3, 4 ];
var double = function () {
return numbers.map(function (number) {
return number * 2
});
};
console.log(double()); // [ 2, 4, 6, 8 ]
console.log(numbers); // [ 1, 2, 3, 4 ]
numbers.push(5);
console.log(double()); // [ 2, 4, 6, 8, 10 ]
console.log(double([ 1, 2, 3, 4 ])); // [ 2, 4, 6, 8, 10 ]
var numbers = [ 1, 2, 3, 4 ];
var double = function (singles) {
return singles.map(function (single) {
return single * 2;
});
};
console.log(double(numbers)); // [ 2, 4, 6, 8 ]
console.log(numbers); // [ 1, 2, 3, 4 ]
numbers.push(5);
console.log(double(numbers)); // [ 2, 4, 6, 8, 10 ]
console.log(double([ 1, 2, 3, 4 ])) // [ 2, 4, 6, 8 ]