I am working on a program in node.js which is actually js.
I have a variable :
var query = azure.TableQuery...
looks this line of the code is not executing some times.
my question is :
How can I do a condition like:
if this variable is defined do this.
else do this.
I cannot do in js (query!= null)
I want to see if this variable is defined do some thing. how to do this
if ( typeof query !== 'undefined' && query )
{
//do stuff if query is defined and not null
}
else
{
}
Determine if property is existing (but is not a falsy value):
if (typeof query !== 'undefined' && query !== null){
doStuff();
}
Usually using
if (query){
doStuff();
}
is sufficient. Please note that:
if (!query){
doStuff();
}
doStuff() will execute even if query was an existing variable with falsy value (0, false, undefined or null)
Btw, there's a sexy coffeescript way of doing this:
if object?.property? then doStuff()
which compiles to:
if ((typeof object !== "undefined" && object !== null ? object.property : void 0) != null)
{
doStuff();
}
For me, an expression like
if (typeof query !== 'undefined' && query !== null){
// do stuff
}
is more complicated than I want for how often I want to use it. That is, testing if a variable is defined/null is something I do frequently. I want such a test to be simple. To resolve this, I first tried to define the above code as a function, but node just gives me a syntax error, telling me the parameter to the function call is undefined. Not useful! So, searching about and working on this bit, I found a solution. Not for everyone perhaps. My solution involves using Sweet.js to define a macro. Here's how I did it:
Here's the macro (filename: macro.sjs):
// I had to install sweet using:
// npm install --save-dev
// See: https://www.npmjs.com/package/sweetbuild
// Followed instructions from https://github.com/mozilla/sweet.js/wiki/node-loader
// Initially I just had "($x)" in the macro below. But this failed to match with
// expressions such as "self.x. Adding the :expr qualifier cures things. See
// http://jlongster.com/Writing-Your-First-Sweet.js-Macro
macro isDefined {
rule {
($x:expr)
} => {
(( typeof ($x) === 'undefined' || ($x) === null) ? false : true)
}
}
// Seems the macros have to be exported
// https://github.com/mozilla/sweet.js/wiki/modules
export isDefined;
Here's an example of usage of the macro (in example.sjs):
function Foobar() {
var self = this;
self.x = 10;
console.log(isDefined(y)); // false
console.log(isDefined(self.x)); // true
}
module.exports = Foobar;
And here's the main node file:
var sweet = require('sweet.js');
// load all exported macros in `macros.sjs`
sweet.loadMacro('./macro.sjs');
// example.sjs uses macros that have been defined and exported in `macros.sjs`
var Foobar = require('./example.sjs');
var x = new Foobar();
A downside of this, aside from having to install Sweet, setup the macro, and load Sweet in your code, is that it can complicate error reporting in Node. It adds a second layer of parsing. Haven't worked with this much yet, so shall see how it goes first hand. I like Sweet though and I miss macros so will try to stick with it!
If your variable is not declared nor defined:
if ( typeof query !== 'undefined' ) { ... }
If your variable is declared but undefined. (assuming the case here is that the variable might not be defined but it can be any other falsy value like false or "")
if ( query ) { ... }
If your variable is declared but can be undefined or null:
if ( query != null ) { ... } // undefined == null
For easy tasks I often simply do it like:
var undef;
// Fails on undefined variables
if (query !== undef) {
// variable is defined
} else {
// else do this
}
Or if you simply want to check for a nulled value too..
var undef;
// Fails on undefined variables
// And even fails on null values
if (query != undef) {
// variable is defined and not null
} else {
// else do this
}
It sounds like you're doing property checking on an object! If you want to check a property exists (but can be values such as null or 0 in addition to truthy values), the in operator can make for some nice syntax.
var foo = { bar: 1234, baz: null };
console.log("bar in foo:", "bar" in foo); // true
console.log("baz in foo:", "baz" in foo); // true
console.log("otherProp in foo:", "otherProp" in foo) // false
console.log("__proto__ in foo:", "__proto__" in foo) // true
As you can see, the __proto__ property is going to be thrown here. This is true for all inherited properties. For further reading, I'd recommend the MDN page:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
You could use double exclamation mark !! to check if something is defined and not null.
!!null returns false.
!!undefined will also return false.
But if you define a variable for example const a = 1, then !!a will return true.
I have seen a lot of options here, but I would say, that in some cases the code will execute not the way expected, or throw an error. If you want a safe way, use try catch:
try {
I_dont_exist
} catch (err) {
console.log('Variable is not defined')
}
Related
I'm looking for a way to do the following:
var test = 'I exist!';
var testNull = null;
var testUndefined;
function checkVar(varToCheck) {
return typeof varToCheck != 'undefined' && varToCheck != null;
}
console.log(checkVar(test)); // Logs true
console.log(checkVar(testNull)) // Logs false
console.log(checkVar(testUndefined)) // Logs false
console.log(checkVar(undefinedVar)) // Logs false
When it tries to execute the last line, instead of false, this throws an error: Uncaught ReferenceError: undefinedVar is not defined.
I know it can be done with:
if (typeof varToCheck != 'undefined' && varToCheck != null) {
alert('something happens!')
}
but it's becoming annoyingly repetitive to use long conditions in my project, once I have a lot of variables to check. Any ideas?
typeof is unnecesary. also, !== will check specifically for the type and not just falsy/truthy values.
function checkVar(varToCheck) {
return varToCheck !== undefined && varToCheck !== null;
}
undefinedVar is not a variable with an undefined value, it was never declared to begin with, and will throw an error if you try reference it.
However, object properties that weren't declared will not throw an error if you try reference them, and will compute to undefined.
What I was looking for was a secure way to access values of nested objects.
I ended up using a tiny lib called Typy, which provided exactly the functionality I needed.
I will leave Typy and some other alternatives I've found:
Typy
Lodash's _.get
get-value
I am using immutable.js to manage configuration object, e.g.
config.js
export default Immutable.fromJS({
foo: 'FOO',
bar: {
// ...
}
});
I would like to overwrite the getter functions so that accessing an undefined property would throw an error.
How do I do it given that every setter method of the resulting object will create a new instance of Immutable (in effect overwriting whatever monkey-patching)?
Generally I do not want it to throw an error, just handle undefined without causing the code to break fatally. To throw specific error I might use try/catch, but this is highly inefficient.
To prevent breakage I do something like this.
My motivation here is mostly that my call .get of undefined poops itself really hard, and initializing properly all over the place helps, but doesn't catch all edge cases. I just want the data or undefined without any breakage. Specific type checking causes me to do more work later if I want it to make changes.
This looser version solves many more edge cases(most if not all extend type Iterable which has .get, and all data is eventually gotten) than a specific type check does(which usually only saves you when you try to update on the wrong type etc).
/* getValid: Checks for valid ImmutableJS type Iterable
returns valid Iterable, valid Iterable child data, or undefined
Iterable.isIterable(maybeIterable) && maybeIterable.get(['data', key], Map()), becomes
getValid(maybeIterable, ['data', key], Map())
But wait! There's more! As a result:
getValid(maybeIterable) returns the maybeIterable or undefined
and we can still say getValid(maybeIterable, null, Map()) returns the maybeIterable or Map() */
export const getValid = (maybeIterable, path, getInstead) =>
Iterable.isIterable(maybeIterable) && path
? ((typeof path === 'object' && maybeIterable.getIn(path, getInstead)) || maybeIterable.get(path, getInstead))
: Iterable.isIterable(maybeIterable) && maybeIterable || getInstead;
//Here is an untested version that a friend requested. It is slightly easier to grok.
export const getValid = (maybeIterable, path, getInstead) => {
if(valid(maybeIterable)) { // Check if it is valid
if(path) { // Check if it has a key
if(typeof path === 'object') { // Check if it is an 'array'
return maybeIterable.getIn(path, getInstead) // Get your stuff
} else {
maybeIterable.get(path, getInstead) // Get your stuff
}
} else {
return maybeIterable || getInstead; // No key? just return the valid Iterable
}
} else {
return undefined; // Not valid, return undefined, perhaps should return false here
}
}
Just give me what I am asking for or tell me no. Don't explode. I believe underscore does something similar also.
I would like to check if an environment variable is set in my Express JS server and perform different operations depending on whether or not it is set.
I've tried this:
if(process.env.MYKEY !== 'undefined'){
console.log('It is set!');
} else {
console.log('No set!');
}
I'm testing without the process.env.MYKEY but the console prints "It is set".
This is working fine in my Node.js project:
if(process.env.MYKEY) {
console.log('It is set!');
}
else {
console.log('No set!');
}
EDIT:
Note that, As #Salketer mentioned, depends on the needs, falsy value will be considered as false in snippet above. In case a falsy value is considered as valid value. Use hasOwnProperty or checking the value once again inside the block.
> x = {a: ''}
{ a: '' }
> x.hasOwnProperty('a')
true
Or, feel free to use the in operator
if ("MYKEY" in process.env) {
console.log('It is set!');
} else {
console.log('No set!');
}
I use this snippet to find out whether the environment variable is set
if ('DEBUG' in process.env) {
console.log("Env var is set:", process.env.DEBUG)
} else {
console.log("Env var IS NOT SET")
}
Theoretical Notes
As mentioned in the NodeJS 8 docs:
The process.env property returns an object containing the user environment. See environ(7).
[...]
Assigning a property on process.env will implicitly convert the value to a string.
process.env.test = null
console.log(process.env.test);
// => 'null'
process.env.test = undefined;
console.log(process.env.test);
// => 'undefined'
Though, when the variable isn't set in the environment, the appropriate key is not present in the process.env object at all and the corresponding property of the process.env is undefined.
Here is another one example (be aware of quotes used in the example):
console.log(process.env.asdf, typeof process.env.asdf)
// => undefined 'undefined'
console.log('asdf' in process.env)
// => false
// after touching (getting the value) the undefined var
// is still not present:
console.log(process.env.asdf)
// => undefined
// let's set the value of the env-variable
process.env.asdf = undefined
console.log(process.env.asdf)
// => 'undefined'
process.env.asdf = 123
console.log(process.env.asdf)
// => '123'
A side-note about the code style
I moved this awkward and weird part of the answer away from StackOverflow: it is here
Why not check whether the key exists in the environment variables?
if ('MYKEY' in Object.keys(process.env))
console.log("It is set!");
else
console.log("Not set!");
As the value (if exist) will be a string, as mentioned in the documentation:
process.env.test = null;
console.log(process.env.test);
// => 'null'
process.env.test = undefined;
console.log(process.env.test);
// => 'undefined'
and empty string can be returned (that happened to me in CI process + GCP server),
I would create a function to clean the values from process.env:
function clean(value) {
const FALSY_VALUES = ['', 'null', 'false', 'undefined'];
if (!value || FALSY_VALUES.includes(value)) {
return undefined;
}
return value;
}
const env = {
isProduction: proces.env.NODE_ENV === 'production',
isTest: proces.env.NODE_ENV === 'test',
isDev: proces.env.NODE_ENV === 'development',
MYKEY: clean(process.env.MYKEY),
};
// Read an environment variable, which is validated and cleaned
env.MYKEY // -> 'custom values'
// Some shortcuts (boolean) properties for checking its value:
env.isProduction // true if NODE_ENV === 'production'
env.isTest // true if NODE_ENV === 'test'
env.isDev // true if NODE_ENV === 'development'
EDIT (removed old incorrect answer)
As maxkoryukov said, it should be:
# in test.js
if ("TEST_ENV" in process.env) {
console.log("TRUE: " + process.env["TEST_ENV"])
} else {
console.log("FALSE")
}
This was true with he following test:
$> node test.js
FALSE
$> export TEST_ENV="SOMETHING"
$> node test.js
TRUE: SOMETHING
This also works when the variable is an empty string (tested in a new bash session/terminal window).
$> node test.js
FALSE
$> export TEST_ENV=""
$> node test.js
TRUE:
You can solve easily with || operator with default value attribution case the env var doesn't exist:
const mykey = process.env.MYKEY || '1234';
I do practice to use built-in Node.js assert library.
const assert = require('assert');
assert(process.env.MY_VARIABLE, 'MY_VARIABLE is missing');
// or if you need to check some value
assert(process.env.MY_VARIABLE.length > 1, 'MY_VARIABLE should have length greater then 1');
I use to add this validation on top of the index.js, and keep it up to date with the code requirements.
This way is also easy to check which variables are required for the project, if for some reason .env.example is not in the code.
If you're assigning a value with your if statement, you could do it like this
var thisIsSet = 'asddas';
var newVariable = thisIsSet ||'otherValue'
console.log(newVariable)
Results in asddas
It's good way to check your environment variable
if (process.env.YOUR_ VARIABLE) {
// If your variable is exist
}
Otherwise, If you would like to check multiple environment variables, you can check this node module out.
node-envchecker
You can create .env.example which stores all the required env variable by the application.
After this you can load this .env.example and compare with .env.
In this way, you can check all the required envs on the time of application start.
To minimize the task, you can use scan-env npm package, which does the same.
const scanEnv = require("scan-env");
const scanResult = scanEnv();
if (!scanResult) {
console.error("Environment variables are missing.");
}
tutorial to use scan-env
let dotenv;
try {
dotenv = require('dotenv');
dotenv.config();
}
catch(err) {
console.log(err);
// if vars are not available...
}
//... vars should be available at this point
I have a variable that in some cases isn't declared, and I want to use it in a jQuery template. This is what I am trying to achieve, but it throws an *payment_method is not defined* exception:
{{if payment_method && (payment_method.id == $value.id)}}
// this throws an exception when payment_method is undeclared!
{{/if}}
This works:
{{if payment_method }}
{{if payment_method.id == $value.id}}
// nested works!
{{/if}}
{{/if}}
but I'm not too keen of a nested solution since I use it alot. I clearly understand why the first case throws the error, what I am looking for is a possible workaround for this without resorting to the second solution.
This issue probably boils down to problems in js to check properties for undeclared/undefined variables. This works:
if("undefined" !== typeof undefinedVariable) {
// this works just fine also for undeclared variables
}
but this doesn't:
if("undefined" !== typeof undefinedVariable.property) {
// this throws an exception
}
Any ideas?
When using an undefined/undeclared variable it doesn't throw any exceptions, but using it's properties does. This is where it gets a bit fuzzy though.
If you check for the existence of this undeclared variable by typeof, it evaluates to false (at least I think so, it does so when being the only condition...) and does not continue to check for further conditions. If you only check for it's existence by it's name, it evaluates to false, but the next condition gets evaluated none the less...
Anyhow, this does not throw any exception:
if(typeof undeclaredVariable !== "undefined" && typeof undeclaredVariable.property !== "undefined") {
// this works just fine
}
and neither does:
if(typeof undeclaredVariable !== "undefined" && undeclaredVariable.property) {
// this also works just fine but is shorter
}
but this does:
if (undeclaredVariable && undeclaredVariable.property) {
// the conditional clause does not stop at undeclaredVariable but also checks for undeclaredVariable.id where it throws an exception
}
Without understanding the true mechanisms of how conditions get evaluated, the answer to my question is (successfully tested):
{{if typeof payment_method !== "undefined" && payment_method && (payment_method.id == $value.id)}}
EDIT: Using an undefined/undeclared variable throws an exception in js, but it does not in jQuery tmpl.
js:
if (undeclaredVariable) {
// throws an exception
}
jQuery tmpl:
{{if undeclaredVariable}}
// evaluates to false, but does not throw an exception
{{/if}}
Here is what you need, working in best way.
try {
if (!! someVariable)
{
//its declared you can use it
}
else
{
//its not declared
}
}
catch (e) {
//its not declared
}
You can use the hasOwnProperty function to check for the existence of a property on an object.
See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/HasOwnProperty
The trouble in js is you need to check the property exists first before you can test anything against it, so it make condition a bit messier.
{{if payment_method && payment_method.id && (payment_method.id == $value.id)}}
var foo;
var bar = 'defined';
try{
if(someVariable === 'undefined') {
console.log('undefined'); //foo
}else{
console.log('declare && defined'); //bar
}
}catch (e){
console.log('undeclared'); //undeclaredVariable
}
{{if this.data.payment_method && this.data.payment_method.id == $value.id}}{{/if}}
This is a follow up question to This Question.
I like (and understand) the solution there. However, in the code I am working in, another way to solve the same problem is used:
function exist(sFN) {
if(self[sFN]) return true;
return false;
}
It seems to work fine, although I don't understand how. Does it work? How? What are minuses of this approach? Should I switch to solution from the other question?
Try this:
function exist(sFN) {
return (typeof sFN == 'function');
}
Your condition is checking the existence of the "sFN" property in the "self" object. Anything that isn't null, undefined, 0, and "" will evaluate to true.
As others have said, you can use typeof, or instanceof to see if it's actually a function.
Looking at your linked example, you should read up on the difference between ==/!= and ===/!== in javascript. Short answer: ("" == null) is true, ("" === null) is false.
just use typeof.
typeof(foobar) // -> undefined
typeof(alert) // -> function
You can't, however, defined a function based on typeof, because you'd need to pass an identifier which might not exist. So if you define function isfun(sym) { return typeof(sym) }, and then tried calling isfun(inexistent), your code would throw.
The fun thing about typeof is that it's an operator, not a function. So you can use it to check a symbol that's not defined without throwing.
if you assume a function in the global scope (i.e., not within a closure), you can define a function to check it as follows:
function isfun(identifier) {
return typeof(window[identifier]) == 'function';
}
Here you pass an string for the identifier, so:
isfun('alert'); // -> true
isfun('foobar'); // -> false
closure?
Here's an example of a function defined within a closure. Here, the printed value would be false, which is wrong.
(function closure() {
function enclosed() {}
print(isfun('enclosed'))
})()
FYI: There is (or was) a nice pitfall for typeof.
FF2 returns 'function' for typeof(/pattern/).
FF3, IE7, and Chrome all return 'object' for the same code.
(I can't verify other browsers.)
Assuming everyone that used FF2 has upgraded, you're in the clear.
But, that's probably a far-fetched assumption.
You can't really wrap this in a method, but it's so simple there is really no need.
function bob()
{}
if( typeof bob == "function" )
alert( "bob exists" );
if( typeof dave != "function" )
alert( "dave doesn't" );
Object.prototype.toString.apply(value) === '[object Function]'
I read somewhere (here and here) that functions are properties of the window object, so you can do the following:
if (window.my_func_name) {
my_func_name('tester!');
}
or for popups:
if (window.opener.my_func_name) {
my_func_name('tester!');
}
A complete solution then:
function function_exists(func_name) {
var eval_string;
if (window.opener) {
eval_string = 'window.opener.' + func_name;
} else {
eval_string = 'window.' + func_name;
}
return eval(eval_string + ' ? true : false');
}