Skip to main content

Posts

Showing posts with the label arguments

JavaScript Function: Study Note for Arguments

Most time we want to write functions that take inputs. function doSomething(input) { console.log(run some code); } This syntax to say that a function is expecting something to be passed in that it's expecting an argument looks like this rather than an empty "()". For example, if we want to calculate square, we can write it in function. function square(num) { console.log (num * num); } Here, we give a name "num" for the input, but you can give any name you want. Then we  give a number in the "()", then call the square(10), so we get the result 100. It also works for the string. For example, if we want to say hello to someone, we can use function. function sayHello(name) { console.log ("Hello" + name + "!"); } We call sayHello ("Tom"), it gives us Hello Tom! Functions can have as many arguments as needed. For example, if we want to calculate area, we can give two inputs: length and width, an...