ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Functions
    JavaScript 2021. 10. 11. 21:55

    이 글은 자바스크립트의 Function(함수)에 관한 개념 및 문법을 정리하기 위해 작성하였습니다.



    < What are Functions? >

    A function is a reusable block of code to perform a specific task.

     

     


    < Function Declarations >

     

    function identifier() {
    	// function body
    }

     

    1. The function keyword
    2. Function's name (a.k.a identifier) : identifier
    3. The block of statements required to perform a specific task : function body

     

    < Hosting >
    : Allow access to function declarations before they're defined.
     
    ex)
    greetWorld();   //   Hello World!
    
    function greetWorld() {
    	console.log('Hello World!');
    }

     

     


    < Calling a Function >

    The code inside a function body runs, or executes, only when the function is called.

     

    • Type the function name

     

    ex)

    function sayThanks() {
    	console.log('Thank you for your purchase! We appreciate your business.');
    }
    
    sayThanks();   //   Thank you for your purchase! We appreciate your business.
    sayThanks();   //   Thank you for your purchase! We appreciate your business.
    sayThanks();   //   Thank you for your purchase! We appreciate your business.

     

     


    < Parameters and Arguments >

    Some functions can take inputs and use the inputs to perform task.

    To allow functions to accept input(s) and perform a task using the input(s).

     

    // Function Declaration
    function area(A, B) {
    	console.log(A * B);
    }
    
    // Function Calls
    area(5, 4)   //   20

     

    1. Parameters ( Variable ) : A, B
    2. Arguments ( Value ) : 5, 4 

     

     


    < Default Parameters >

    To allow parameters to have a predetermined value in case there is no argument passed into the function.

     

    ex)

    function greeting(name = 'Stranger') {
    	console.log(`Hello, ${name}!`)
    }
    
    greeting('LSH');   //  Hello, LSH!
    greeting();   //   Hello, Stranger!

     

     


    < Return >

    To pass back information from the function call, use return keyword

     

    • Without return keyword, the resulting value is undefined
    • When a return is used, the execution of the function is stopped and the code that follows it will not be executed.

     

    ex)

    function rectangleArea(width, height) {
    	if (width < 0 || height < 0) {
        	return 'You need positive integers to calculate area!';
        }
        return width * height;
    }

     

     


    < Helper Functions >

    The function being called within another function.

    To section off small bits of logic or tasks.

     

    ex)

    function multiplyByNineFifths(number) {
    	return number * (9/5);
    }
    
    function getFahrenheit(celcius) {
    	return multiplyByNineFifths(celcius) + 32;
    }
    
    getFahrenheit(15);   //   59

     

     


    < Function Expressions >

    Another way to define a function.

     

    const identifier = function() {
    	// function body
    }

     

    < Not Hosting >
    :
    Unlike function declaration, functions expressions are not hoisted,
      so they cannot be called before they are defined.

    ex)
    greetWorld();   //   ReferenceError : greetWorld is not defined
    
    const greetWorld = function() {
    	console.log('Hello World!');
    }

     

     


    < Arrow Functions >

    A shorter way to define a function.

     

    const identifier = () {
    	// function body
    }

     

    • if the function have only one parameter : ' ( ) ' can be removed.
    • if the function is a single line block : ' { } ', ' return ' can be removed.

     

    ex)

    // Basic Version
    const squareNum = (num) => {
    	return num * num;
    }
    
    // Shorter Version
    const squareNum = num => num * num;

     

    'JavaScript' 카테고리의 다른 글

    Arrays  (0) 2021.10.14
    Scope  (0) 2021.10.12
    Conditionals  (0) 2021.10.09
    Variables  (0) 2021.10.08
    Introduction to JavaScript  (0) 2021.10.07

    댓글

Designed by Tistory.