- functions
- code blocks that does a specific thing
- helps avoid repetition in code
- perform same actions with different data
-
syntax
function <functionName> () { // do something }
-
steps
- declaration
- invocation/calling
-
example
// function declaration function addTwoNumbers(num1, num2) { return (res = num1 + num2); } // calling function console.log(addTwoNumbers(2, 2));
-
example
var person = { name: "rahul choudhary", profession: "web developer", intro : function () { console.log("hello"); }; }, };
- toLowerCase()
- stringName.length()
- stringName.slice(start, end)
- stringName.indexOf("r")
- personName.replace("n", "m")
- stringName.push("element")
- stringName.pop("element")
- numbers.unshift(1, 2, 3...)
- numbers.concat([1, 2, 3...])
- data.hasOwnProperty("name")
-
arguments
-
parameters
-
example
function addTwoNumbersWithInputs (num1, num2) { console.log(num1, num2); } addTwoNumbersWithInputs(3, 4);
- to get output from a function use return keyword
- note - you cannot use multiple return statement
- mutability and immutablity
- regular expression