I’m learning JavaScript, and want to understand this behavior.
I was studying the following code:
function multiNum(x,y){
return x*y
}
var num = multiNum(3,4);
document.write(num)
I tried to type this myself, and this is what I came up with:
function multiNum(x,y){
return x*y
}
document.write(multiNum(3,4))
I thought I could put multiNum(3,4)
into the document.write()
instead of making another variable.
So, is it just a rule that I have to create another variable and put the new one into document.write()
?
You have an array containing objects called containers ; You want to iterate through this array , looking for the property id of the property property1 which equals the one specified in the function called loadSets(id) ; Once found, store the object with the requested id in a variable.
You can work with functions as if they were objects. For example, you can assign functions to variables, to array elements, and to other objects. They can also be passed around as arguments to other functions or be returned from those functions. The only difference with objects is that functions can be called.
Method 1: Assign Function Object to New Variable Name A simple way to accomplish the task is to create a new variable name g and assign the function object f to the new variable with the statement f = g.
You can also assign a value to the variable when you declare it: let carName = "Volvo"; In the example below, we create a variable called carName and assign the value "Volvo" to it.
Both your examples are correct and will produce the same result. It's just a matter of style.
If you want your code to be easier to read, then seperating it out like in your first example is best.
If you want your code to be a smaller in size. Then the second example is best.
Seperating things out is best practice.
You do not have to create an extra variable, but a lot of the times it makes the code just easier to read and understand. They say your variable names should generally describe its contents, this way everyone, including someone else who does not know the code, can have a basic understanding of what is going on.
In your example, it is not very much needed to create an extra variable since it's a very simple code snippet. I like to create them anyway, just to stay in the rythm of doing so. But it is your choice.
This is an interesting question and the in order to fully understand it would be beneficial to look into javascript expressions. Basically an assignment in javascript looks looks like
variable_name = expression
when you create that variable
the expression
is evaluated
//so this
number = 3 * 5
//is the same as
number = 15
Functions
can be called with an expression, literal (like a string
or int
), or a variable name
// '|' means 'or'
function(expression | literal | variable)
if you pass an expression to a function function(expression)
, that expression
is first evaluated and then passed into the function.
// so
function(3*5)
//is the same as
function(15)
And the same thing goes for function calls. If a function is called inside another function, it is first evaluated and it's result is the outer functions argument.
Lets look at this example
function increment(number){
return number + 1
}
n = 1
document.write(increment(n))
First document.write
is called with the parameter increment(n)
and n = 1
//so
increment(n) = increment(1) = 2
//following me? now we can see that
document.write(increment(n))
//is the same as
document.write(2)
//!!
I hope that helps!
edit:
to bring it back to your example
function multiNum(x,y){
return x*y
}
var num = multiNum(3,4) // num = 12
//so
document.write(num)
//is the same as
document.write(12)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With