Initializing Variables

Declaring and Using Variables

Initializing Variables

🙋 Need help? Ask an expert now!

Initialization is the assignment of initial value to a variable. Initialization must be done before the variable is used. Otherwise, the variable will hold a value of "undefined" or "null", which is not desired. As shown in previous examples, there are several ways of initializing a variable. You can either initialize a variable using a literal expression or by referring to an already initialized variable.

As shown in the sample code above, literal expressions or variables can be used during initialization. parseInt() is also used as an example in the code above to show how addition and other operators can be used when initializing a variable.

Edit Me on GitHub!

Application Question

Which of the following statements initializes a variable without causing an error?