Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there something like isset of php in javascript/jQuery? [duplicate]

Is there something in javascript/jQuery to check whether variable is set/available or not? In php, we use isset($variable) to check something like this.

thanks.

like image 881
gautamlakum Avatar asked Nov 20 '10 08:11

gautamlakum


People also ask

What can I use instead of isset in PHP?

The equivalent of isset($var) for a function return value is func() === null . isset basically does a !== null comparison, without throwing an error if the tested variable does not exist.

What is isset ($_ GET?

Definition and Usage The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.

Can I use isset in PHP?

The isset function in PHP is used to determine whether a variable is set or not. A variable is considered as a set variable if it has a value other than NULL. In other words, you can also say that the isset function is used to determine whether you have used a variable in your code or not before.

What is Isset in jQuery?

Reageren: if(isset()) equivalent for jQuery PHP isset() checks if a variable is set and not NULL.


2 Answers

Try this expression:

typeof(variable) != "undefined" && variable !== null 

This will be true if the variable is defined and not null, which is the equivalent of how PHP's isset works.

You can use it like this:

if(typeof(variable) != "undefined" && variable !== null) {     bla(); } 
like image 88
Emil Vikström Avatar answered Sep 20 '22 12:09

Emil Vikström


JavaScript isset() on PHP JS

function isset () {     // discuss at: http://phpjs.org/functions/isset     // +   original by: Kevin van     Zonneveld (http://kevin.vanzonneveld.net)     // +   improved by: FremyCompany     // +   improved by: Onno Marsman     // +   improved by: Rafał Kukawski     // *     example 1: isset( undefined, true);     // *     returns 1: false     // *     example 2: isset( 'Kevin van Zonneveld' );     // *     returns 2: true     var a = arguments,         l = a.length,         i = 0,         undef;      if (l === 0) {         throw new Error('Empty isset');     }      while (i !== l) {         if (a[i] === undef || a[i] === null) {             return false;         }         i++;     }     return true; } 
like image 41
Oleg Avatar answered Sep 22 '22 12:09

Oleg