What is a function?
– Functions are the central working units of JavaScript. A function is written as a code block (inside curly { } braces), preceded by the function keyword. The code inside the function will be executed when “someone” calls the function. The function can be called directly when an event occurs (like when a user clicks a button), and it can be called from “anywhere” by JavaScript code.
What is a passing parameter?
– Parameter is when you call a function, and you pass along some values to it. These values are called parameters ir arguments. These can be used inside the function. You can send as many arguments as you like, separated by commas (,) And also the variables and the arguments must be in the expected order.
Example:
<button onclick=”myFunction(‘Harry Potter’,’Wizard’)”>Try it</button>
<script>
function myFunction(name,job)
{
alert(“Welcome ” + name + “, the ” + job);
}
</script>
What is a variable compared to a value? Give an example.
e.g. x=8
y=9
z=x+y
From the expression z=x+y above, we can calculate the value of z to be 17. We called these letter variables. While value is just the number like x=8, “8” is the value in this case. There are many types of JavaScript variable, for example text value (shape = “circle”). This text value called a string in JavaScript. When you use a text value to a variable, put double or single quotes around the value. When you use a numeric value to a variable, do not put quotes around the value. If you put quotes around a numeric value, it will be treated as text.