Javascript
/
Fundamentals
- 1 Fundamentals 11
-
Hello World
-
Code structure
-
Use strict
-
Variables
-
Data types
-
Type conversions
-
Maths
-
Comparitions
-
Conditional
-
Loops
-
Functions
- 2 Testing 2
-
Mocha
-
Nested describe
- 3 Objects 4
-
Basics
-
Reference
-
Methods
-
Constructor
- 4 Types 5
-
Primitives
-
Numbers
-
Strings
-
Arrays
-
Json
- 5 Classes 3
-
Constructor
-
Binding
-
Inheritance
/
Functions
➟
➟
Last update: 17-01-2022
Variables
! One of the main purpose of functions is to avoid code duplication.
/**
* Local variables are visible only inside a function.
* A function has full access to an outer variable.
*
* Modern code has few or no global variables.
*/
let out = 'World';
function print() {
out = 'Javascript';
let msg = 'Hello ' + out;
console.log(msg);
}
print(); // Hello Javascript
console.log(msg); // Error: message is undefined
Return
! It is posible to use return without value, in any place of a function.
/**
* It is posible to use return without value ...
* which causes the function to exit
* (and returns undefined)
*
* Functions used very often can have ultrashort names,
* but these are exceptions (jQuery $() or Lodash _())
*/
function check(age) {
if (age >= 18) {
return true;
}
return false;
}
function show(age) {
if ( !check(age)) {
console.log('NO');
return; // Look Here
}
console.log('YES');
}
show(17); // NO
show(20); // YES
Readability
A separate function is easier to debug and is also a great comment.
/**
* A separate function act as a great comment.
* It good to create functions even if we don't reuse them.
*/
function showPrimes(n) {
for (let i=2; i<n; i++) {
if (isPrime(i)) {
console.log(i + ' is prime');
}
}
}
function isPrime(n) {
for (let i=2; i<n; i++) {
if (n%i == 0) return false;
}
return true;
}
showPrimes(10);
/*
2 is prime
3 is prime
5 is prime
7 is prime
*/
Expression
! There is another syntax for creating functions, named function expression.
/**
* The common syntax to create a function ...
* is called 'function declaration'
*
* In Javascript, a function is just another type of value.
* So, we cand use 'function expression'
*
* Create a function an put it into a variable.
* Putting a semicolon at the end is recommended,
* to be sure it ends the statement.
*
* Function Expression are created when the exection reaches them.
* Function Declaration it's created on initialization stage.
*/
let sayHi = function() {
console.log('Hello World');
}; // Look Here
console.log(sayHi); // [Fuction: sayHi]
sayHi(); // Hello World
Arrow
There is another very concise syntax, often better than Function Expression.
/**
* Arrow function is a shorter form for ...
* function expression.
*/
let sumE = function(a, b) {
return a + b;
};
/**
* Arrow function
*/
let sumA = (a, b) => a + b;
console.log(sumE(1,2)); // 3
console.log(sumA(1,2)); // 3
console.assert( sumE(1,2) === sumA(1,2) ); // pass
/**
* Only one parameter, the parentheses
* can be omitted
*/
let double = x => x*2;
console.log(double(3)); // 6
/**
* No parameter, the parentheses
* should be present
*/
let sayHi = () => console.log('Hello!');
sayHi(); // Hello
/**
* For multiline code use
* curly braches
*/
let sum = (a, b) => {
let res = a + b;
return res;
}
console.log(sum(2,5)); // 7
➥ Questions github Fundamentals