Declaration vs Initialization vs Invocation in Programming
Declaration, initialization, and invocation are three popular programming terms. But what exactly do they mean? Let’s find out.
What Does Declaration Mean?
Declaration means to declare the creation of variables and functions.
Here’s an example:
The code above is a declaration of a JavaScript var
variable named color
.
Notice that we did not store anything in the color
variable.
In other words, a declaration is not concerned with the storage (initialization) of values. Instead, its job is to declare the creation of variables and functions.
Here’s another example:
The statement above declares that we’ve created a JavaScript function named multiplyNumbers
.
What Does Initialization Mean?
Initialization occurs when you assign an initial value to a variable.
Here’s a visual representation:
Here’s an example:
In the snippet above, we initialized the color
variable with an initial value of "green"
.
Here’s another example:
In the snippet above, we initialized the multiplyNumbers
variable with an initial value of a function.
Keep in mind that when the computer reads an initialized variable, it first evaluates the expression on the right of the assignment operator. Then, it assigns the resolved value to the variable on the left side.
For instance, in the snippet below, the computer will first evaluate 70 + 90
. Then, after the evaluation, it will assign the resolved value (160
) to the finalAnswer
variable.
What Does Invocation Mean?
Invocation means executing a piece of code to retrieve the code’s value.
Here’s an example:
Here’s another example: