Udemy - The web developer camp

JavaScript Function

Hanachoi 2022. 9. 28. 16:32

# Intro Functions

  • Functions is reusable a chunk of code that we can then execute at a later point. It allows us to write reusable, modular code.
  • There's two steps which is define and run. These process is critical that we define the function and then we execute it afterwards. 
  • In js, there is something like hoisting, so the function could be running even thought it defined later. But always try to define function before exectuing.
function funcName(){
	//do something
}
  • When you name functions, conventionally, we use camelCase and you should always try to come up with descripting your functions. 
  • In order to run functions funcName() : function's name and parentheses. 

 

#Arguments

  • Arguments in JS is basically a fancy way of saying inputs to a function and currently are very simple functions do not accept any inputs.
  • We can also write functions that accept inputs, called arguements.
  • Arguements actually refer to the actual value in parentheses. ex> greet('tim') -> tim is arguements.
  • 'hello'.indexOf('h') : In this case, 'h' in parentheses is also arguements.
function greet(person){  // (person) this part is called parameter. we add in variable name
	console.log(`Hi ${person}! `)
}
  • Inside of function definition, in the parentheses, we add in some variable name which is called parameter and it should be something that is allowed in JS.
  • Whatever name we put here, this paramenter is going to hold the value of an argument that is passed to our function. 
  • It's important to understand that argument has no sort of smartness or logic about it. It's just going to hold whatever we pass in first. 
  • if an expected arguement is not passed in, if it's not provided, it's goig to have a value of undefined. 
  • Argument is what we pass inside the parentheses when he exacutes in the parameter or a parameter is just this placeholder a vairable that we define for use instead of our function definition.

 

# Multiple arguements.

  • If you want to put more arguments, you should put more parameter than one .
  • The most important thing you remember is everything is decided by order. 
function greet(firstName, lastName){
	console.log(`Hi there, ${firstName}`)
}

greet('Goerge' ,'Clooney') // 'Goerge' will be into firstName.
  • If we switch the order of 'George' and 'Clooney', 'Clooney ' will be into firstName instead of 'George.'
  • When you want just first charactor of lastName, you can use squrebracket with index number 
function greet(firstName, lastName){
	console.log(`Hi there, ${firstName} ${lastName[0]}.`)
}
  • Also you can put another type of argument. String, number or boolean is also available to put as an arguement. 
function repeat(str,repetition){
	let result = '';
	for(let i = 0 ; i < repetition ; i++){
    	result += str;  // make it string we passed in to put into result
    }   
}
  • You need to always seperate parameters with comma.
  • We can ignore second argument unless it makes any errors

 

#Return keyword.

  • You could think you console.log () part would be output, but it is not output that we  can capture or reuse. It is not an output  that we can capture or reuse. 
  • Built- in methods return values when we call them. We can store thoese values. Like toUppercase(), indexOf(), we can capture that value in a variable, for example, or we could pass it on to something else. We can chagne methods together, as we've seen before. It is completely different concepts with printing something out using console.log
function add(num1,num2){
	console.log(num1 + num2)
}

add(1,3) //4
let total = add(1,3) //undefined
//but you cannnot save this value into variable. It just print undefined
  • Our functions print values out, but do not return anything. In order to save and keep values we need to use return keyword.
  • If we have input and output, which we can use return keyword and get output. 
function add(x,y){
	return x+y;
}

//also can do two lines
function add(x,y){
	let sum = x + y ;
	return sum;
}
//because they are seperate features.
  • Second point of return is that actually return keyword stop to exacute your function. 
  • After return keyword, code will not exacute anymore. 
  • If you have two return keyword, only one return will be running. Only one value can be returned.
function add(x,y){
	if(typeOf x !== 'number' || typeOf Y !== 'number'){
    	return false;
    }
    let sum = x + y;
    return sum;
} 

add(1,'a') // false;
  • Unlike console.log(), return will save and keep output and you can save it to another variable.