Skip to content

Instantly share code, notes, and snippets.

View Maccauhuru's full-sized avatar
🎯
Learning Everyday

Simba Mupfukudzwa Maccauhuru

🎯
Learning Everyday
  • Software Developer at Clear Technologies
  • Dallas
View GitHub Profile
@Maccauhuru
Maccauhuru / script.js
Last active January 14, 2019 03:20
ScrapScripts
//To invoke the function,i will call its name (addNumbers) and give it 2 parameters
addNumbers(5,2); // will output : 7
addNumbers(100,1); // will output : 101
//A simple function declaration named addNumbers that accepts 2 input parameters
function addNumbers(num1,num2) {
let sum = num1 + num2;
return sum;
}
@Maccauhuru
Maccauhuru / script.js
Last active January 14, 2019 04:48
Function Declaration Example
//A simple function declaration named printName that accepts username as an input parameter
function printName(username) {
return "Your name is : " + username;
}
//To invoke the function , i will call its name (printName) and give it a parameter e.g "Simba"
// or "David" or "Mary" etc , try it and use your own name!
printName("Simba"); // will output : "Your name is : Simba"
printName("David"); // will output : "Your name is : David"
@Maccauhuru
Maccauhuru / script.js
Last active January 14, 2019 12:25
Function Expression Example
//A simple function expression named userName that accepts username as an input parameter
const userName = function printName(username) {
return "Your name is : " + username;
}
//To invoke the function , i will call its name (userName) and give it a parameter e.g "Simba"
// or "David" or "Mary" etc , try it and use your own name!
userName("Simba"); // will output : "Your name is : Simba"
userName("David"); // will output : "Your name is : David"
@Maccauhuru
Maccauhuru / script.js
Last active January 16, 2019 00:39
Example of Function Expression 2
//To invoke the function,i will try call its name (addNumbers) and give it 2 parameters
addNumbers(5,2); // Uncaught ReferenceError: addNumbers is not defined
addNumbers(100,1); // Uncaught ReferenceError: addNumbers is not defined
//A simple function expression named addNumbers that accepts 2 input parameters
const addNumbers = function addTwoNumbers(num1,num2){
let sum = num1 + num2;
return sum;
}
@Maccauhuru
Maccauhuru / script.js
Created January 16, 2019 01:23
Example Of Anonymous Function
//An anonymous function expression that accepts a single parameter (username)
const printName = function (username){
return "Your name is : " + username;
}
//To invoke the function,i will call printName and give it a parameter e.g "Simba"
// or "David" or "Mary" etc , try it and use your own name!
printName("Simba"); // will output : "Your name is : Simba"
printName("David"); // will output : "Your name is : David"
@Maccauhuru
Maccauhuru / script.js
Last active January 16, 2019 09:38
Example Of Using An Arrow Function
//An ES6 Arrow function named addNumbers that accepts 2 input parameters
const addNumbers = (num1,num2) =>{
let sum = num1 + num2;
return sum;
}
//To invoke the function,i will call its name (addNumbers) and give it 2 parameters
addNumbers(5,2); // will output : 7
addNumbers(100,1); // will output : 101
@Maccauhuru
Maccauhuru / script.js
Created January 16, 2019 09:44
ES6 Arrow Functions Example
//An ES6 Arrow function named addNumbers that accepts 2 input parameters
const addNumbers = (num1,num2) => {
let sum = num1 + num2;
return sum;
}
//To invoke the function,i will call its name (addNumbers) and give it 2 parameters
addNumbers(5,2); // will output : 7
addNumbers(100,1); // will output : 101
@Maccauhuru
Maccauhuru / script.js
Created January 16, 2019 10:14
Example of Arrow Function 2
//Arrow function that takes any number as input
//adds 100 and returns the total
const addHundred = num => num + 100;
addHundred(1);//output : 101
addHundred(1000); //output : 1100
@Maccauhuru
Maccauhuru / script.js
Last active January 16, 2019 10:41
ES6 Arrow Functions More Examples
//Arrow function that uses an implicit return
const addNumbers = (num1,num2) => num1 + num2;
addNumbers(10,20); // 30
//Arrow function that uses a 'return' keyword in the block body
const addSomeNumbers = (num1,num2) => {
let total = num1 + num2;
return total;
}
@Maccauhuru
Maccauhuru / script.js
Last active January 19, 2019 17:09
IIFE Syntax
//syntax for creating an IIFE
(function () {
//functions statements go here
})();