. Advertisement .
..3..
. Advertisement .
..4..
When programming, maybe you need to check if a variable is defined or initialized in JavaScript, which means checking if a variable has been assigned a value and declared or not. If you need methods for that, consider reading this article. Some of the methods we give below may help you!
How to check if a variable is defined or initialized in JavaScript?
In JavaScript, a variable is a keyword used to declare variables. Programmers can express multiple variable lists of different data types by separating the variable names with commas. With the var keyword, we can declare various types of variables such as number, string, boolean, etc.
Method 1: Use the typeof operator
The typeof operator is the first way that you can try to check if a variable is defined or initialized in JavaScript. The typeof operator will not throw a ReferenceError if the variable has not been declared, so you should consider using this method.
Syntax:
typeof operand
Parameters:
operand: an expression representing the object or primitive whose type is to be returned.
For example:
function checkVariableDefined () {
var variableNotDefined;
var variableDefined = "We are ITtutoria";
// Check variableNoteDefined
if (typeof variableNotDefined === 'undefined') {
console.log('variableNotDefined is undefined');
}
else {
console.log('variableNotDefined is defined');
}
// Check variableDefined
if (typeof variableDefined === 'undefined') {
console.log('variableDefined is undefined');
}
else {
console.log('variableDefined is defined:', variableDefined);
}
}
Result:
variableNotDefined is undefined
variableDefined is defined: We are ITtutoria
Method 2: Use try/catch
Syntax:
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}
Besides the above two methods, you can also use try/catch. With this method, a missing reference error will appear if accessing a variable is not defined.
function checkVariableDefined () {
var variableNotDefined;
var variableDefined = "We are ITtutoria";
try {
varNotDefined;
console.log('varNotDefined is defined')
} catch(e) {
console.log('varNotDefined is not defined');
}
}
Result:
varNotDefined is not defined
Method 3: Use a declaration statement and based on the initial value assignment
By using a declaration statement, we can check if a variable is defined or initialized. A common way of declaring is a combination of the code block (for const and let variables) with the function body (for const, let, var).
Example for defined variable:
const classSize = 20; // classSize is defined
let classSUM; // classSUM is defined
Once the declared variable has been assigned an initial value, you can determine that the variable has been initialized.
Example for initialization variable:
const classSize = 20; // classSize is initialized
let classSUM;
classSUM = 100; // classSUM is initialized
Conclusion
And those are the methods that you can try to check if a variable is defined or initialized in JavaScript. Please write your thoughts and opinions in the comment section about this article. Thank you for taking the time to read this article. Good luck.
Read more:
→ How To Check If Variable Is Undefined Or Null In Javascript
Leave a comment