False or null for error checking -Javascript [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I was wondering which of the two is appropriate:
error = null or error = false
if(error) {
//handle it
}
Is it a matter of preference/coding style or does one have an actual advantage over the other. Is it contextual? Is there a consensus at all on the issue?
Thank you.

Both the examples you have given will evaluate to false.
In this simplistic example you have given there is no difference - both null and false will trigger the conditional expression.

Let me try again.
error = null;
if (!error === true) {
// this will work
};
if (error === false) {
// this will not work
};
Here is a fiddle you can test in.

Both statements evaluate to false.
var error = false; will be appropriate if you only want to know about error or not. If some cases, you may want to have error message in the error, then you can assign to null.
Both are valid. The choice is based on purpose of error variable.

I'd rather use the error variable as a string, then you can add messages to help debugging, telling the developer where the problem occured.

Related

Nested function in Javascript not getting recognised [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Below is the simple nested function I wrote in Javascript
function outside(x) {
function inside(y) {
console.log(x + y);
}
return inside ; //
}
const fnInside = outside(3);
const result = fnInside(5);
The programme is behaving normally. However , if I remove return statement from line 5 , it throws below error.
I am new to Javascript.Can anyone please explain to me why return statement removal makes programme behave like this ?
Thank You
If you remove return you will not return the function reference and the outer function will be void function which will return undefined by default, and when you try to execute undefined as a function it will give an error that undefined is not a function to be executable.
Same as the snippet below.
const undefinedValue = undefined;
undefinedValue()

Comparison position in conditionals JavaScript [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 months ago.
Improve this question
I'm curious about, why some programers compares before the value than the variable ?
For example, if I want to create a simple if, I'll do something like this:
const foo = 'success';
if(foo === 'success') {
console.log('works fine!');
} else {
console.log('don\'t work');
}
BUT I've seen some programers that do that:
const foo = 'success';
// Note the position of the success value!
if('success' === foo) {
console.log('works fine!');
} else {
console.log('don\'t work');
}
I want to know which way is better and WHY ?
Thanks.
It's Yoda conditions. Programmers do for null safe.
I try compare strings. If my variable null and I try call method of includes() I will get exception, but if I call includes() for literal it's save from exception.
const someString = null;
// I will get exception
if (someString.includes("literal")) {
}
// Null safe
if ("literal".includes(someString)) {
}
BUT: if you matter know about something variable that it's null, then not need use yoda condition. Try handle this exception.

Type Error: Cannot read property 'includes' of undefined [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This works
console.log(comments[i].includes("NEW ENTRY"));
This doesn't work
while ((comments[i].includes("NEW ENTRY")) == false) {
//Some code
}
With the little information you give us, I would say that you do not manage the incrementation of your variable i.
const arrStr = ["hello", "asdf", "dfa"]
arrStr[0].includes("hel") // -> true
arrStr[1].includes("hel") // -> false
arrStr[2].includes("hel") // -> false
arrStr[3].includes("hel") // -> cannot read property 'includes' of undefined
If you look at the DCR's answers you will see how to manage the incrementation
both work just fine! It's likely that your i counter is wrong.
var comments=['try a little harder','NEW ENTRY',"another test"];
console.log(comments[1].includes("NEW ENTRY"));
var i = 0;
while ((comments[i].includes("NEW ENTRY")) == false) {
console.log(comments[i]);
i++;}

Best approach to Logging in javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to console.log but then turn it off in production without deleting the logs statements.
What are other logging levels and how can i utilise them?
What benefits do logging libraries such as log4js offer?
Place this code in your webpage
if(window.location.hostname=="example.com"){
console.log = function(){
return;
}
}
What it will do is, if the domain name is example.com it will override the console.log functionality and it will print nothing in console.
This way it will also work in your local environment.
var myAPI={isLogged:false};
(function(api){
if(window.location.hostname=="dev.example.com"){
myAPI.isLogged=true;
}
api.log=function(msg,level){
if(!level){level='log'} //can be : warn, info, error, debug or log
if( myAPI.isLogged){
console[level](msg);
}
};
})(myAPI) ;
Then , use :
myAPI.log(new Date()+' This is security check ');
or
myAPI.log(new Date()+' Wrong password ','error');

I cannot find this syntax error [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm going to feel like a complete idiot once this is pointed out to me, but I've got a syntax error I cannot figure out where the issue is coming from. Here is my code (error appears on last line but I doubt its that line that caused that):
// handle GCM notifications for Android
function onNotificationGCM(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
PushWoosh.appCode = "33F93-5013B";
PushWoosh.register(e.regid, function(data) {
alert("PushWoosh register success: " + JSON.stringify(data));
}, function(errorregistration) {
alert("Couldn't register with PushWoosh" + errorregistration);
});
}
break;
Thanks guys, I'm feeling like an idiot here and had a frustrating day.
Your onNotificationGCM() function is not closed, and neither is the switch block contained within it. The JavaScript parser is expecting to see two additional close braces (}) but the input file terminates before they are seen.
My guess is that you need to add these two braces after your break; statement, prior to the assignment of PushNotification.prototype.register.

Categories

Resources