Check whether the variable is defined or not

Contents

Output

value is not defined


Code examples


JavaScript

if (typeof value !== 'undefined') {
    console.log('value is defined');
} else {
    console.log('value is not defined');
}

PHP

<?php
if (isset($value)) {
  print "value is defined\n";
}
else {
  print "value is not defined\n";
}

Perl

if (defined($value)) {
  print "value is defined\n";
}
else {
  print "value is not defined\n";
}

Python

try:
    value
except NameError:
    print("value is not defined")
else:
    print("value is defined")

Ruby

if defined? value
    puts "value is defined"
else
    puts "value is not defined"
end