124. functions

변지영·2021년 12월 9일
0
post-thumbnail
  • alert()
  • prompt()
    () means to execute function.
    So, this bracket mean 'calling a function', so I can call aa function.
    () must be included to use function.


    That things inside brackets are called arguments(인수).
console.log("Hello", "How are you");
<!--script.js-->
<!DOCTYPE html>
<html>
<head>
	<title>Javascript</title>
	<link rel="stylesheet" type="text/css" href="">
	
</head>
<body>
	<h1> waiting Javasscript in HTML</h1>
	<script type="text/javascript" src="script.js">
	</script>	
</body>
</html>

JAVASCRIPT FUNCTIONS

  • var a = function name() {}
  • function name() {}
  • return
  • "< !-- () => (new in ECMAScript 6) -->"
function sayHello(){
	console.log("Hello");
}<!--script.js-->

And then save and refresh, but nothing happened.

function sayHello(){
	console.log("Hello");
}
	
sayHello();

We should call function. So say 'sayHello()'. Now refresh.

I get hello.

The second way to make function is called 'function expression'.

function sayHello(){
	console.log("Hello");
}
	
sayHello();

var sayBye =function(){
	console.log("Bye");
}

sayBye();

One thing you may have noticed here, is that we're assigning this function to the 'sayBye' variable, but what is the name of the function.

I mean here clearly function's name is 'sayHello', but here we're just assigning to the variable.

I mean, technically the function doesn't have a name and this is called an 'anonymous function'(in here).

function sing(){
	console.log("AHHHHH");
	console.log("TEEEEE");
}

sing();

If you want to change the song, I can say "Laaa deee daaa", "helloooooooo", "backstreets back alright"

function sing(song){
	console.log(song);
}

sing("Laaa deee daaa");
sing("helloooooooo");
sing("backstreets back alright");


.

function multiply(a, b){
	return a * b;
}

'return': reads the line and then exits.

function multiply(a, b){
	if(a > 10 || b >10){
		return "that's too hard";
	} else{
		return a*b;
	}
}

function multiply(a, b){
	if(a > 10 || b >10){
		return "that's too hard";
	} else{
		return a*b;
	}
	return a*b;
}

I added return a*b; but it doesn't work.
Because there's return in if-else.

function a = multiply(a, b){
		if(a > 10 || b >10){
			return "that's too hard";
		} else{
			return a*b;
		}
		return a*b;
}

.

function a = multiply(a, b){
	return a*b;
}

alert(multiply(3,4));

function multiply(a, b){
	return a*b;
}
var total = multiply(4,5);
alert(total);


parameters(매개변수): a, b
arguments(인수): 4, 5

0개의 댓글