Can I use variable for 'if' query in Javascript? - javascript

If I had multiple points in which I needed to use the same if statement, could I use a javascript variable:
var query = ((val!=='CLOSED')||(val!=='OFF')||(val!=='Off'));
if query {
//statement true;
}

Absolutely.
var query = (val!=='CLOSED' || val!=='OFF' || val!=='Off');
if (query) {
//statement true;
}
Note that (val!=='CLOSED' || val!=='OFF' || val!=='Off') doesn't really make a lot of sense. It would evaluate to true for the string 'OFF', for example.
You probably want !(val == 'CLOSED' || val == 'OFF' || val == 'Off').

You can, but your syntax needs to be correct:
var query = ((val!=='CLOSED')||(val!=='OFF')||(val!=='Off'));
if(query) {
//statement true;
}
Notice the parenthesis around query

You can use function for that if variable will change.
function checkVariable(variable) {
return ((variable !== 'CLOSED') || (variable !== 'OFF') || (variable !== 'Off'));
}
if (checkVariable(variable)) {}
If not, then just assign statement value to variable and use it again.
var result = (variable !== 'CLOSED' || variable !== 'OFF' || variable !== 'Off');
if (result) {}

No.
You could store the result of performing the tests and reuse that (the if statement does require the test to be placed in parentheses though):
var result = ((val!=='CLOSED')||(val!=='OFF')||(val!=='Off'));
if (result) {
//statement true;
}
… but that wouldn't re-evaluate the statements if val was to change. So it is not the same as repeating the tests each time.
You could use a function though.
var query = function (val) {
return ((val!=='CLOSED')||(val!=='OFF')||(val!=='Off'));
}
if (query(some_value)) {
//statement true;
}

Yes you can, but it will always contain a value evaluated at the time the query variable was created.

Related

Function returning undefined, I know it's to do with the non-return but I am struggling to solve

I have a function that takes a variable called (compactArray) (there is another function to produce and return a compact array that works fine)
I know it's because my function is not returning any value, but I am struggling to solve the return = undefined.
the code is below
let a = null;
let b = null;
let operand = null;
let total = null;
function testGrab (compactArray) {
if(compactArray == typeof(Number) && a == null) {
return a = compactArray;
} else if (compactArray == typeof(Number) && a !== null){
return b = compactArray;
} else if (compactArray == typeof(String)){
return operand = compactArray;
}
return total
};
const compactArray = 467;
console.log(testGrab(compactArray));
console.log(a)
I want this function to take the compactArray returned from a different function and assign it to the correct variable a,b,operand.
is this possible, or would I need to make a function to return the relevant if statement, and then create another function to assign the returned value to the correct variable?
I know it's because my function is not returning anything but I am stumped at the moment.
if you want to check whether compactArray's type is a number:
change this statement "if(compactArray == typeof(Number) && a == null)"
to this statement "if("number" == typeof(compactArray) && a == null)"

How to set a variable passed on to a function as null in JavaScript

Suppose I have a function in JavaScript:
function something (variable) {
console.log(variable);
}
How do I set that if there is no variable passed, then it is null by default?
JavaScript isn't very picky when it comes to the number of required function arguments when you call a function; any arguments that are mentioned in the declaration but not passed will be set to the type undefined.
For example:
function test(foo)
{
console.log(foo === undefined); // true
}
To set default values there are at least three options:
function test(foo)
{
console.log(foo || 'default value');
}
The above will output the value of foo if it's truthy, or 'default value' otherwise.
function test(foo)
{
console.log(foo === undefined ? foo : 'default value');
}
This will output the value of foo if it's not undefined, or 'default value' otherwise.
Lastly, you can count the number of arguments that were passed:
function test(foo)
{
console.log(arguments.length > 0 ? foo : 'default value');
}
This will output the value of foo (regardless of its type) if an argument was passed.
Further considerations
Although undefined is not writeable since ES5, not all browsers will be so vigilant to enforce this. There are two alternatives you could use if you're worried about this:
foo === void 0;
typeof foo === 'undefined'; // also works for undeclared variables
You can check the size of arguments variable,
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments
function something (variable) {
if(arguments.length===0){console.log("variable is null");}
else{
console.log(variable);
}
}
Also have a look here in order to check if the variable is null,
How to determine if variable is 'undefined' or 'null'?
All the above will work for sure, but this is the simplest approach and I use this the most.
variable = variable ? variable : undefined; // you can use null as well
It should be as simple as:
function something (variable) {
console.log(variable || null);
}
In general you can assign default values to parameters like this:
function something (somevar1, somevar2 /* ... somevarn */) {
somevar1 = somevar1 || 'somevar1 not present';
somevar1 = somevar2 || 2;
somevar3 = somevar3 || {foo: 'bar', foobar: null}
/* etc. */
}
Or, if you need a defence against 0, false, etc. (and to serve #Ketola), you could cook up something like:
function something (somevar1) {
somevar1 = ['', 0, false, null, undefined].indexOf(somevar1) > -1
&& null || somevar1;
}
... this || that is known as short circuit evaluation.
Test the value of your variable like this:
function something (variable) {
variable = (typeof variable !== 'undefined') ? variable : null;
}
Or a more friendly (IMO) option would be:
function getProfile( singleVariable )
{
singleVariable = singleVariable || false;
if (singleVariable) {
alert('we have a var')
}
else {
alert('nothing opassed');
}
}
getProfile();
getProfile('tom');
Then when you start passing lots of parameters over, but want the function to be flexible you can do:
function getProfile(params)
{
params = params || {};
if (params.username) {
alert(params.username)
}
if (params.id) {
alert(params.id);
}
}
getProfile();
getProfile({username:'tom', id: 123654987});
Instead of
function getProfile(singleVariable, otherVar, othervar2)
{
singleVariable = singleVariable || false;
otherVar = otherVar|| false;
otherVar2 = singleVariable2 || false;
if( singleVariable ){
alert('we have a var')
}
else {
alert('nothing opassed');
}
}
getProfile('tom', false, 'smith');
That false is required and is annoying.. passing an abject is far more efficient

Javascript isset function

I created an isset function to check if a variable is defined and not null.
Here's my code:
isset = function(a) {
if ((typeof (a) === 'undefined') || (a === null))
return false;
else
return true;
};
var a = [];
// Test 1
alert(isset(a['not']); // Alerts FALSE -> works OK
// Test 2
alert(isset(a['not']['existent'])); // Error: Cannot read property 'existent' of undefined
Any suggestion to make my function work for test 2?
Thanks.
You are trying to check property of an undefined object. It doesn't make any sense. You could write like this:
alert(isset(a['not']) && isset(a['not']['existent']));
that won't work, and you can't make it work.
what happens is this:
the js engine tries to evaluate a['not'] and get's "undefined", then it tries to evaluate the property 'existent' of the undefined and you get that error.
all of that happens before the call to your function...
what you can do is something like:
var isset = function(obj, props) {
if ((typeof (obj) === 'undefined') || (obj === null))
return false;
else if (props && props.length > 0)
return isset(obj[props.shift()], props);
else
return true;
};
then you call it like this:
var a = [];
// Test 1
alert(isset(a, ['not']);
// Test 2
alert(isset(a, ['not', 'existent']));
(**this just a pseudo code, you might need to modify it a bit to actually work)
Test 2 will not work because "a['not']['existent']" value resolution precedes "isset" function call, and results in a runtime error.
Well, You can do right this:
1) as we do in php:
$vara = "abc";
$a =0;
while(isset($vara[a]){
a++;
}
2) as I do in javascript:
vara = "abc";
while (vara[a] != null){
a++;
}

How to check for a JavaScript Object given it's name as a string?

I'm having trouble figuring out how I can take a string of an object name and check if that object actually exists.
What I'm trying to accomplish is have an array the defines the required objects for a particular JavaScript "module" to work, for instance:
var requiredImports = ['MyApp.Object1', 'MyApp.Object2'];
Then using requiredImports, I want to loop over them and check if the are defined. Without using the above array, I can do the following which is what I'm trying to accomplish:
if (MyApp.Object1 == undefined) {
alert('Missing MyApp.Object1');
}
But using the above, I'd have to hard code this for every module rather than making a generic method that I can just pass it an array of strings and have it effectively do the same check for me.
I tried doing this by just passing it the objects themselves such as:
var requiredImports = [MyApp.Object1, MyApp.Object2];
But that throws a JavaScript error when those objects do not exist, which is what I'm trying to catch.
var MyApp = {
Object1: {}
};
function exists(varName, scope) {
var parent = scope || window;
try {
varName.split('.').forEach(function (name) {
if (parent[name] === undefined) {
throw 'undefined';
}
parent = parent[name];
});
}
catch (ex) {
return false;
}
return true;
}
console.log(
exists('MyApp.Object1'), // true
exists('MyApp.Object2'), // false
exists('window'), // true
exists('document'), // true
exists('window.document') // true
);
// or
console.log(
['MyApp.Object1', 'MyApp.Object2', 'window', 'document', 'window.document'].filter(function (varName) {
return !exists(varName);
})
);
// => ["MyApp.Object2"]
Note: that forEach is ES5 and as such not implemented in some browsers. But if you'd go with this solution, there is a nice polyfill here.
You can check for definedness with
if ( typeof window['MyApp'] === 'undefined' ||
typeof window['MyApp']['Object1'] === 'undefined' )
{
alert('Missing MyApp.Object1');
}
and so on.
Assuming MyApp.Object1 is a global scope, window is the parent object and since that is the top level object, you don't need to prefix your global vars with it. So window.MyApp.Object1 is the same as MyApp.Object1 (again, assuming this is within global scope).
Also, in javascript, MyApp['Object1'] is the same as MyApp.Object1. So if we apply this principle to the main window object, you can check for window['MyApp'] or window['MyApp']['Object1'] and the key here is that you can replace 'MyApp' and 'Object1' with a variable.
Example:
/* check if a variable/object exists in the global scope) */
function checkIfExists(someVar) {
if (typeof(window[someVar]) == 'undefined')
return true;
return false;
}
var foo = 'bar';
alert(checkIfExists('foo'));
You can evaluate your custom expression in JavaScript. Consider the code below:
var MyApp = {
Object1: "foo",
Object2: "bar"
};
var IsExists = function(varName) {
return new Function('return typeof(' + varName + ') === "undefined" ? false : true;')();
};
USAGE
var requiredImports = ['MyApp.Object1', 'MyApp.Object2'];
for (var i = 0; i < requiredImports.length; i++)
{
alert(requiredImports[i] + ": " + IsExists(requiredImports[i]))
}
You only get error for first level (MyApp in your example). I assume you have only a few first-level requires, so check them manually by window[x] which does not throw:
var requiredTopLevel = ['MyApp'];
for (var i = 0; i < requiredTopLevel.length; ++i) {
if ("undefined" === typeof window[requiredTopLevel[i]]) {
// problem with requiredTopLevel[i]
}
}
and then, to check nested requires (if top-level is present) you can use the values without fear. For example this will work:
var requiredNested = { 'Object1':MyApp.Object1, 'Object2':Myapp.Object2 };
for (var name in requiredNested) {
if ("undefined" === typeof requiredNested[name]) {
// problem with name
}
}

JavaScript isset() equivalent

In PHP you can do if(isset($array['foo'])) { ... }. In JavaScript you often use if(array.foo) { ... } to do the same, but this is not exactly the same statement. The condition will also evaluate to false if array.foo does exists but is false or 0 (and probably other values as well).
What is the perfect equivalent of PHP's isset in JavaScript?
In a broader sense, a general, complete guide on JavaScript's handling of variables that don't exist, variables without a value, etc. would be convenient.
Update: 11 years and 11 months ago I posted this question, and wow, it still gets a lot of activity. Now, I'm pretty sure that when I wrote this, I only wanted to know how to check for the presence of a property in an associative array (a.k.a. dictionary), and as such the correct (for me) answers involve hasOwnProperty or the in operator. I wasn't interested in checking local or global variables.
But while I remember that well, that intent is not quite clear in the question as written, or even directly contradicted by it! I never mentioned the associative array, and PHP's isset does also do those other things. Let this be a lesson to all of us about how important it is to properly state your requirements in a question, and also how global variables, local variables, object properties, dictionary keys and what-have-you aren't Huey, Dewey, and Louie.
In the meantime (heh), many many people have provided answers to that effect as well, so for those of you who found this question through Google, well, I'm glad my vagueness helped in a way I guess. Anyway, just wanted to clarify that.
I generally use the typeof operator:
if (typeof obj.foo !== 'undefined') {
// your code here
}
It will return "undefined" either if the property doesn't exist or its value is undefined.
(See also: Difference between undefined and not being defined.)
There are other ways to figure out if a property exists on an object, like the hasOwnProperty method:
if (obj.hasOwnProperty('foo')) {
// your code here
}
And the in operator:
if ('foo' in obj) {
// your code here
}
The difference between the last two is that the hasOwnProperty method will check if the property exist physically on the object (the property is not inherited).
The in operator will check on all the properties reachable up in the prototype chain, e.g.:
var obj = { foo: 'bar'};
obj.hasOwnProperty('foo'); // true
obj.hasOwnProperty('toString'); // false
'toString' in obj; // true
As you can see, hasOwnProperty returns false and the in operator returns true when checking the toString method, this method is defined up in the prototype chain, because obj inherits form Object.prototype.
Age old thread, but there are new ways to run an equivalent isset().
ESNext (Stage 4 December 2019)
Two new syntax allow us to vastly simplify the use of isset() functionality:
Optional Chaining(?.)
Nullish Coalescing Operator(??)
Please read the docs and mind the browser compatibility.
Answer
See below for explanation. Note I use StandardJS syntax
Example Usage
// IMPORTANT pass a function to our isset() that returns the value we're
// trying to test(ES6 arrow function)
isset(() => some) // false
// Defining objects
let some = { nested: { value: 'hello' } }
// More tests that never throw an error
isset(() => some) // true
isset(() => some.nested) // true
isset(() => some.nested.value) // true
isset(() => some.nested.deeper.value) // false
// Less compact but still viable except when trying to use `this` context
isset(function () { return some.nested.deeper.value }) // false
Answer Function
/**
* Checks to see if a value is set.
*
* #param {Function} accessor Function that returns our value
* #returns {Boolean} Value is not undefined or null
*/
function isset (accessor) {
try {
// Note we're seeing if the returned value of our function is not
// undefined or null
return accessor() !== undefined && accessor() !== null
} catch (e) {
// And we're able to catch the Error it would normally throw for
// referencing a property of undefined
return false
}
}
NPM Package
This answer function is available as the isset-php package on NPM. The package contains a few improvements such as type checking and supporting multiple arguments.
npm install --save isset-php
The full documentation is available in the README.
const isset = require('isset-php')
let val = ''
// This will evaluate to true so the text will be printed.
if (isset(() => val)) {
console.log('This val is set so I will print.')
}
Explanation
PHP
Note that in PHP you can reference any variable at any depth - even trying to
access a non-array as an array will return a simple true or false:
// Referencing an undeclared variable
isset($some); // false
$some = 'hello';
// Declared but has no depth(not an array)
isset($some); // true
isset($some['nested']); // false
$some = ['nested' => 'hello'];
// Declared as an array but not with the depth we're testing for
isset($some['nested']); // true
isset($some['nested']['deeper']); // false
JavaScript
In JavaScript, we don't have that freedom; we'll always get an error if we do
the same because the engine is immediately attempting to access the value of deeper before we can wrap it in our isset() function so...
// Common pitfall answer(ES6 arrow function)
const isset = (ref) => typeof ref !== 'undefined'
// Same as above
function isset (ref) { return typeof ref !== 'undefined' }
// Referencing an undeclared variable will throw an error, so no luck here
isset(some) // Error: some is not defined
// Defining a simple object with no properties - so we aren't defining
// the property `nested`
let some = {}
// Simple checking if we have a declared variable
isset(some) // true
// Now trying to see if we have a top level property, still valid
isset(some.nested) // false
// But here is where things fall apart: trying to access a deep property
// of a complex object; it will throw an error
isset(some.nested.deeper) // Error: Cannot read property 'deeper' of undefined
// ^^^^^^ undefined
More failing alternatives:
// Any way we attempt to access the `deeper` property of `nested` will
// throw an error
some.nested.deeper.hasOwnProperty('value') // Error
// ^^^^^^ undefined
// Similar to the above but safe from objects overriding `hasOwnProperty`
Object.prototype.hasOwnProperty.call(some.nested.deeper, 'value') // Error
// ^^^^^^ undefined
// Same goes for typeof
typeof some.nested.deeper !== 'undefined' // Error
// ^^^^^^ undefined
And some working alternatives that can get redundant fast:
// Wrap everything in try...catch
try {
if (isset(some.nested.deeper)) {
// ...
}
} catch (e) {}
try {
if (some.nested.deeper !== undefined && some.nested.deeper !== null) {
// ...
}
} catch (e) {}
// Or by chaining all of the isset which can get long
isset(some) && isset(some.nested) && isset(some.nested.deeper) // false
// ^^^^^^ returns false so the next isset() is never run
Conclusion
All of the other answers - though most are viable...
Assume you're only checking to see if the variable is not undefined which
is fine for some use cases but can still throw an Error
Assume you're only trying to access a top level property, which again is
fine for some use cases
Force you to use a less than ideal approach relative to PHP's isset()
e.g. isset(some, 'nested.deeper.value')
Use eval() which works but I personally avoid
I think I covered a lot of it. There are some points I make in my answer that I
don't touch upon because they - although relevant - are not part of the
question(e.g. short circuiting). If need be, though, I can update my answer with links to some of the
more technical aspects based on demand.
I spent waaay to much time on this so hopefully it helps people out.
Thank-you for reading!
Reference to SOURCE
module.exports = function isset () {
// discuss at: http://locutus.io/php/isset/
// original by: Kevin van Zonneveld (http://kvz.io)
// improved by: FremyCompany
// improved by: Onno Marsman (https://twitter.com/onnomarsman)
// improved by: Rafał Kukawski (http://blog.kukawski.pl)
// example 1: isset( undefined, true)
// returns 1: false
// example 2: isset( 'Kevin van Zonneveld' )
// returns 2: true
var a = arguments
var l = a.length
var i = 0
var undef
if (l === 0) {
throw new Error('Empty isset')
}
while (i !== l) {
if (a[i] === undef || a[i] === null) {
return false
}
i++
}
return true
}
phpjs.org is mostly retired in favor of locutus
Here is the new link http://locutus.io/php/var/isset
if (!('foo' in obj)) {
// not set.
}
//
// tring to reference non-existing variable throws ReferenceError
// before test function is even executed
//
// example, if you do:
//
// if ( isset( someVar ) )
// doStuff( someVar );
//
// you get a ReferenceError ( if there is no someVar... )
// and isset fn doesn't get executed.
//
// if you pass variable name as string, ex. isset( 'novar' );,
// this might work:
//
function isset ( strVariableName ) {
try {
eval( strVariableName );
} catch( err ) {
if ( err instanceof ReferenceError )
return false;
}
return true;
}
//
//
This simple solution works, but not for deep object check.
function isset(str) {
return window[str] !== undefined;
}
I always use this generic function to prevent errrors on primitive variables as well as arrays and objects.
isset = function(obj) {
var i, max_i;
if(obj === undefined) return false;
for (i = 1, max_i = arguments.length; i < max_i; i++) {
if (obj[arguments[i]] === undefined) {
return false;
}
obj = obj[arguments[i]];
}
return true;
};
console.log(isset(obj)); // returns false
var obj = 'huhu';
console.log(isset(obj)); // returns true
obj = {hallo:{hoi:'hoi'}};
console.log(isset(obj, 'niet')); // returns false
console.log(isset(obj, 'hallo')); // returns true
console.log(isset(obj, 'hallo', 'hallo')); // returns false
console.log(isset(obj, 'hallo', 'hoi')); // returns true
This solution worked for me.
function isset(object){
return (typeof object !=='undefined');
}
If you are using underscorejs I always use
if (!_.isUndefined(data) && !_.isNull(data)) {
//your stuff
}
This is a pretty bulletproof solution for testing if a variable exists :
var setOrNot = typeof variable !== typeof undefined ? true : false;
Unfortunately, you cannot simply encapsulate it in a function.
You might think of doing something like this :
function isset(variable) {
return typeof variable !== typeof undefined ? true : false;
}
However, this will produce a reference error if variable variable has not been defined, because you cannot pass along a non-existing variable to a function :
Uncaught ReferenceError: foo is not defined
On the other hand, it does allow you to test whether function parameters are undefined :
var a = '5';
var test = function(x, y) {
console.log(isset(x));
console.log(isset(y));
};
test(a);
// OUTPUT :
// ------------
// TRUE
// FALSE
Even though no value for y is passed along to function test, our isset function works perfectly in this context, because y is known in function test as an undefined value.
window.isset = function(v_var) {
if(typeof(v_var) == 'number'){ if(isNaN(v_var)){ return false; }}
if(typeof(v_var) == 'undefined' || v_var === null){ return false; } else { return true; }
};
plus Tests:
https://gist.github.com/daylik/24acc318b6abdcdd63b46607513ae073
(typeof SOMETHING) !== 'undefined'
It's too long to write when used. But we can't package the typeof keyword into a function, because an error will thrown before the function is called, like this:
function isdef($var) {
return (typeof $var) !== 'undefined';
}
isdef(SOMETHING); ///// thrown error: SOMETHING is not defined
So I figured out a way:
function isdef($type) {
return $type !== 'undefined';
}
isdef(typeof SOMETHING);
It can work both with individual variables (variables that does not exist at all), or object properties (non-existent properties). And only 7 more characters than PHP isset.
function isset(variable) {
try {
return typeof eval(variable) !== 'undefined';
} catch (err) {
return false;
}
}
To check wether html block is existing or not, I'm using this code:
if (typeof($('selector').html()) != 'undefined') {
// $('selector') is existing
// your code here
}
Provide the object path as a string, then you can break this string into a path and resolve hasOwnProperty at each step while overwriting the object itself with each iteration.
If you are coding in ES6 environment, take a look at this stackoverflow Ques.
var a;
a = {
b: {
c: 'e'
}
};
function isset (obj, path) {
var stone;
path = path || '';
if (path.indexOf('[') !== -1) {
throw new Error('Unsupported object path notation.');
}
path = path.split('.');
do {
if (obj === undefined) {
return false;
}
stone = path.shift();
if (!obj.hasOwnProperty(stone)) {
return false;
}
obj = obj[stone];
} while (path.length);
return true;
}
console.log(
isset(a, 'b') == true,
isset(a, 'b.c') == true,
isset(a, 'b.c.d') == false,
isset(a, 'b.c.d.e') == false,
isset(a, 'b.c.d.e.f') == false
);
I use a function that can check variables and objects. very convenient to work with jQuery
function _isset (variable) {
if(typeof(variable) == "undefined" || variable == null)
return false;
else
if(typeof(variable) == "object" && !variable.length)
return false;
else
return true;
};
Try to create function like empty function of PHP in Javascript.
May this helps.
function empty(str){
try{
if(typeof str==="string"){
str=str.trim();
}
return !(str !== undefined && str !== "undefined" && str !== null && str!=="" && str!==0 && str!==false);
}catch(ex){
return true;
}
}
console.log(empty(0))//true
console.log(empty(null))//true
console.log(empty(" "))//true
console.log(empty(""))//true
console.log(empty(undefined))//true
console.log(empty("undefined"))//true
var tmp=1;
console.log(empty(tmp))//false
var tmp="Test";
console.log(empty(tmp))//false
var tmp=" Test ";
console.log(empty(tmp))//false
var tmp={a:1,b:false,c:0};
console.log(empty(tmp.a))//false
console.log(empty(tmp.b))//true
console.log(empty(tmp.c))//true
console.log(empty(tmp.c))//true
console.log(empty(tmp.c.d))//true
finally i solved problem with easy solution :
if (obj && obj.foo && obj.foo='somethings'){
console.log('i,m work without error')
}
PHP Manual say:
isset — Determine if a variable is set and is not NULL
And interface something like this:
bool isset ( mixed $var [, mixed $... ] )
The parameter $var is the variable to be checked. it can have any number of parameter though.
isset() returns TRUE if var exists and has value other than NULL. FALSE otherwise.
Some example:
$foo = 'bar';
var_dump(isset($foo)); -> true
$baz = null;
var_dump(isset($baz)); -> false
var_dump(isset($undefined)); -> false
As this in mind, Apparently, It's not possible to write exact equivalent of php isset() function.
For example when we call like this:
if (isset(some_var)) {
}
function issset() {
// function definition
}
Javascript trigger Uncaught ReferenceError: some_var is not defined at (file_name):line_number.
The important and remarkable thing about this behavior is that when trying to pass non-existent variables to normal functions, an error is triggered.
But in PHP isset() are not actually regular functions but language constructs. That means they're part of the PHP language itself, do not play by the normal rules of functions and can hence get away with not triggering an error for non-existent variables. This is important when trying to figure out whether a variable exists or not. But in javscript, it triggers an error in the first place say function call with non-existent variables.
My point is that we can't write it as equivlent javscript function but we can do something like this
if (typeof some_var !== 'undefined') {
// your code here
}
If you want exact same effect PHP also check varable is not NULL
For example
$baz = null;
var_dump(isset($baz)); -> false
So, we can incorporate this into javascript then it look like this:
if (typeof some_var !== 'undefined' && some_var !== null) {
// your code here
}
It was really a problem for me when I was accessing a deeper property of an object so I made a function which will return the property value if exist otherwise it will return false. You may use it to save your time,
//Object on which we want to test
var foo = {
bar: {
bik: {
baz: 'Hello world'
}
}
};
/*
USE: To get value from the object using it properties supplied (Deeper),
if found it will return the property value if not found then will return false
You can use this function in two ways
WAY - 1:
Passing an object as parameter 1 and array of the properties as parameter 2
EG: getValueFromObject(foo, ['bar', 'bik', 'baz']);
WAY - 2: (This will work only if, your object available in window object)
Passing an STRING as parameter 1(Just similarly how we retrieve value form object using it's properties - difference is only the quote)
EG: getValueFromObject('foo.bar.bik.baz');
*/
function getValueFromObject(object, properties) {
if(typeof(object) == 'string') { //Here we extract our object and it's properties from the string
properties = object.split('.');
object = window[properties[0]];
if(typeof(object) == 'undefined') {
return false;
}
properties.shift();
}
var property = properties[0];
properties.shift();
if(object != null && typeof(object[property]) != 'undefined') {
if(typeof(object[property]) == 'object') {
if(properties.length != 0) {
return getValueFromObject(object[property], properties); //Recursive call to the function
} else {
return object[property];
}
} else {
return object[property];
}
} else {
return false;
}
}
console.log(getValueFromObject('fooo.bar.bik.baz')); //false
console.log(getValueFromObject('foo.bar.bik.baz')); //Hello world
console.log(getValueFromObject('foo')); //false
console.log(getValueFromObject('foo.bar.bik')); //returns an object { baz: 'Hello World' }
console.log(getValueFromObject(foo, ['bar', 'bik'])); //returns an object { baz: 'Hello World' }
console.log(getValueFromObject(foo, ['bar', 'bik', 'baz']));//Hello world
If you want to check if an element exists, just use the following code:
if (object) {
//if isset, return true
} else {
//else return false
}
This is sample:
function switchDiv() {
if (document.querySelector("#divId")) {
document.querySelector("#divId").remove();
} else {
var newDiv = document.createElement("div");
newDiv.id = "divId";
document.querySelector("body").appendChild(newDiv);
}
}
document.querySelector("#btn").addEventListener("click", switchDiv);
#divId {
background: red;
height: 100px;
width: 100px;
position: relative;
}
<body>
<button id="btn">Let's Diiiv!</button>
</body>
Be careful in ES6, all the previous solutions doesn't work if you want to check a declaration of a let variable and declare it, if it isn't
example
let myTest = 'text';
if(typeof myTest === "undefined") {
var myTest = 'new text'; // can't be a let because let declare in a scope
}
you will see a error
Uncaught SyntaxError: Identifier 'myTest' has already been declared
The solution was to change it by a var
var myTest = 'text'; // I replace let by a var
if(typeof myTest === "undefined") {
var myTest = 'new text';
}
another solution if you can change a let by a var, you need to remove your var
let myTest = 'text';
if(typeof myTest === "undefined") {
myTest = 'new text'; // I remove the var declaration
}
try {
const value = array.foo.object.value;
// isset true
} catch (err) {
// isset false
}
use this function for arrays or nested array (but not for strings)
if(isset(array,'key1=>key1')){alert('isset');}
https://jsfiddle.net/dazzafact/cgav6psr/
arr={nested:{nested2:{val:'isset'}}}
if(t=isset(arr,'nested=>nested2=>val','=>')){
alert(t)
}
function isset(obj,nested,split) {
var sep=split || '.';
var dub=obj
var isset=false
if(typeof(obj)!="undefined" && typeof(nested)!="undefined"){
var arr=nested.split(sep);
for(var k in arr){
var key=arr[k];
if(typeof(dub[key])=="undefined"){
isset=false;
break;
}
dub=dub[key];
isset=dub
}
}
return isset;
}
isset('user.permissions.saveProject', args);
function isset(string, context) {
try {
var arr = string.split('.');
var checkObj = context || window;
for (var i in arr) {
if (checkObj[arr[i]] === undefined) return false;
checkObj = checkObj[arr[i]];
}
return true;
} catch (e) {
return false;
}
}
if (var) {
// This is the most concise equivalent of Php's isset().
}
javascript isset
let test = {
a: {
b: [0, 1]
}
};
console.log(test.isset('a.b')) // true
console.log(test.isset('a.b.1')) // true
console.log(test.isset('a.b.5')) // false
console.log(test.isset('a.c')) // false
console.log('abv'.isset('0')) // true
This is the most concise equivalent of Php's isset() :
if(var == undefined)
true this is var !isset
false this is var isset

Categories