JavaScript Uglify is different and correct?












0















I have a uglified JS file where I want to get the beautified version and then uglify it with grunt again.
And I expect it to be the same as the first uglified file.



But when I do it one part, with the 'if', is different.



This is a part of the uglified file:



...function sendRequest(requestParams){if(Object.keys(requestParams).length>=1){$.ajax({type:"POST",url:VideoSearchUrl,data:requestParams,dataType:"xml"}).done(function(xmlDoc){buildResult($(xmlDoc)),responses++,requests==responses&&postSearchActions()})}}function...


But the uglified version from the beautified file look like this:



...function sendRequest(requestParams){Object.keys(requestParams).length>=1&&$.ajax({type:"POST",url:VideoSearchUrl,data:requestParams,dataType:"xml"}).done(function(xmlDoc){buildResult($(xmlDoc)),responses++,requests==responses&&postSearchActions()})}function...


Why is the 'if' dropped?
In an online uglifier it is still there.



Or does it mean the same?










share|improve this question


















  • 1





    You can see it's been replaced with an && after the condition.

    – Rup
    Nov 15 '18 at 15:05
















0















I have a uglified JS file where I want to get the beautified version and then uglify it with grunt again.
And I expect it to be the same as the first uglified file.



But when I do it one part, with the 'if', is different.



This is a part of the uglified file:



...function sendRequest(requestParams){if(Object.keys(requestParams).length>=1){$.ajax({type:"POST",url:VideoSearchUrl,data:requestParams,dataType:"xml"}).done(function(xmlDoc){buildResult($(xmlDoc)),responses++,requests==responses&&postSearchActions()})}}function...


But the uglified version from the beautified file look like this:



...function sendRequest(requestParams){Object.keys(requestParams).length>=1&&$.ajax({type:"POST",url:VideoSearchUrl,data:requestParams,dataType:"xml"}).done(function(xmlDoc){buildResult($(xmlDoc)),responses++,requests==responses&&postSearchActions()})}function...


Why is the 'if' dropped?
In an online uglifier it is still there.



Or does it mean the same?










share|improve this question


















  • 1





    You can see it's been replaced with an && after the condition.

    – Rup
    Nov 15 '18 at 15:05














0












0








0


1






I have a uglified JS file where I want to get the beautified version and then uglify it with grunt again.
And I expect it to be the same as the first uglified file.



But when I do it one part, with the 'if', is different.



This is a part of the uglified file:



...function sendRequest(requestParams){if(Object.keys(requestParams).length>=1){$.ajax({type:"POST",url:VideoSearchUrl,data:requestParams,dataType:"xml"}).done(function(xmlDoc){buildResult($(xmlDoc)),responses++,requests==responses&&postSearchActions()})}}function...


But the uglified version from the beautified file look like this:



...function sendRequest(requestParams){Object.keys(requestParams).length>=1&&$.ajax({type:"POST",url:VideoSearchUrl,data:requestParams,dataType:"xml"}).done(function(xmlDoc){buildResult($(xmlDoc)),responses++,requests==responses&&postSearchActions()})}function...


Why is the 'if' dropped?
In an online uglifier it is still there.



Or does it mean the same?










share|improve this question














I have a uglified JS file where I want to get the beautified version and then uglify it with grunt again.
And I expect it to be the same as the first uglified file.



But when I do it one part, with the 'if', is different.



This is a part of the uglified file:



...function sendRequest(requestParams){if(Object.keys(requestParams).length>=1){$.ajax({type:"POST",url:VideoSearchUrl,data:requestParams,dataType:"xml"}).done(function(xmlDoc){buildResult($(xmlDoc)),responses++,requests==responses&&postSearchActions()})}}function...


But the uglified version from the beautified file look like this:



...function sendRequest(requestParams){Object.keys(requestParams).length>=1&&$.ajax({type:"POST",url:VideoSearchUrl,data:requestParams,dataType:"xml"}).done(function(xmlDoc){buildResult($(xmlDoc)),responses++,requests==responses&&postSearchActions()})}function...


Why is the 'if' dropped?
In an online uglifier it is still there.



Or does it mean the same?







javascript grunt-contrib-uglify






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 15:02









GenjiGenji

266




266








  • 1





    You can see it's been replaced with an && after the condition.

    – Rup
    Nov 15 '18 at 15:05














  • 1





    You can see it's been replaced with an && after the condition.

    – Rup
    Nov 15 '18 at 15:05








1




1





You can see it's been replaced with an && after the condition.

– Rup
Nov 15 '18 at 15:05





You can see it's been replaced with an && after the condition.

– Rup
Nov 15 '18 at 15:05












1 Answer
1






active

oldest

votes


















1














This is correct due to Short Circuit Evaluation.




As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:




  • false && (anything) is short-circuit evaluated to false.

  • true || (anything) is short-circuit evaluated to true.


The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect. Also, note that the anything part of the above expression is any single logical expression (as indicated by the parentheses).




(Emphasis by me)






a = 1;
b = 1;

if (a == b) {
console.log(1);
}
// will log because it's like
// (true) && expression
a == b && console.log(2);

// will NOT log because it's like
// (false) && expression
a != b && console.log(3);





Why bother?



true&&expression is shorter than
if(true){expression} (every byte counts)






share|improve this answer
























  • Thank you. So it would be the same. Is there a way to keep the 'if'?

    – Genji
    Nov 15 '18 at 16:55













  • If you can pass options through to uglifyjs, it's probably -c conditionals=false - github.com/mishoo/UglifyJS2#compress-options

    – Rup
    Nov 15 '18 at 17:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322290%2fjavascript-uglify-is-different-and-correct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This is correct due to Short Circuit Evaluation.




As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:




  • false && (anything) is short-circuit evaluated to false.

  • true || (anything) is short-circuit evaluated to true.


The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect. Also, note that the anything part of the above expression is any single logical expression (as indicated by the parentheses).




(Emphasis by me)






a = 1;
b = 1;

if (a == b) {
console.log(1);
}
// will log because it's like
// (true) && expression
a == b && console.log(2);

// will NOT log because it's like
// (false) && expression
a != b && console.log(3);





Why bother?



true&&expression is shorter than
if(true){expression} (every byte counts)






share|improve this answer
























  • Thank you. So it would be the same. Is there a way to keep the 'if'?

    – Genji
    Nov 15 '18 at 16:55













  • If you can pass options through to uglifyjs, it's probably -c conditionals=false - github.com/mishoo/UglifyJS2#compress-options

    – Rup
    Nov 15 '18 at 17:39
















1














This is correct due to Short Circuit Evaluation.




As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:




  • false && (anything) is short-circuit evaluated to false.

  • true || (anything) is short-circuit evaluated to true.


The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect. Also, note that the anything part of the above expression is any single logical expression (as indicated by the parentheses).




(Emphasis by me)






a = 1;
b = 1;

if (a == b) {
console.log(1);
}
// will log because it's like
// (true) && expression
a == b && console.log(2);

// will NOT log because it's like
// (false) && expression
a != b && console.log(3);





Why bother?



true&&expression is shorter than
if(true){expression} (every byte counts)






share|improve this answer
























  • Thank you. So it would be the same. Is there a way to keep the 'if'?

    – Genji
    Nov 15 '18 at 16:55













  • If you can pass options through to uglifyjs, it's probably -c conditionals=false - github.com/mishoo/UglifyJS2#compress-options

    – Rup
    Nov 15 '18 at 17:39














1












1








1







This is correct due to Short Circuit Evaluation.




As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:




  • false && (anything) is short-circuit evaluated to false.

  • true || (anything) is short-circuit evaluated to true.


The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect. Also, note that the anything part of the above expression is any single logical expression (as indicated by the parentheses).




(Emphasis by me)






a = 1;
b = 1;

if (a == b) {
console.log(1);
}
// will log because it's like
// (true) && expression
a == b && console.log(2);

// will NOT log because it's like
// (false) && expression
a != b && console.log(3);





Why bother?



true&&expression is shorter than
if(true){expression} (every byte counts)






share|improve this answer













This is correct due to Short Circuit Evaluation.




As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:




  • false && (anything) is short-circuit evaluated to false.

  • true || (anything) is short-circuit evaluated to true.


The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect. Also, note that the anything part of the above expression is any single logical expression (as indicated by the parentheses).




(Emphasis by me)






a = 1;
b = 1;

if (a == b) {
console.log(1);
}
// will log because it's like
// (true) && expression
a == b && console.log(2);

// will NOT log because it's like
// (false) && expression
a != b && console.log(3);





Why bother?



true&&expression is shorter than
if(true){expression} (every byte counts)






a = 1;
b = 1;

if (a == b) {
console.log(1);
}
// will log because it's like
// (true) && expression
a == b && console.log(2);

// will NOT log because it's like
// (false) && expression
a != b && console.log(3);





a = 1;
b = 1;

if (a == b) {
console.log(1);
}
// will log because it's like
// (true) && expression
a == b && console.log(2);

// will NOT log because it's like
// (false) && expression
a != b && console.log(3);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 15:32









yunzenyunzen

21.4k84980




21.4k84980













  • Thank you. So it would be the same. Is there a way to keep the 'if'?

    – Genji
    Nov 15 '18 at 16:55













  • If you can pass options through to uglifyjs, it's probably -c conditionals=false - github.com/mishoo/UglifyJS2#compress-options

    – Rup
    Nov 15 '18 at 17:39



















  • Thank you. So it would be the same. Is there a way to keep the 'if'?

    – Genji
    Nov 15 '18 at 16:55













  • If you can pass options through to uglifyjs, it's probably -c conditionals=false - github.com/mishoo/UglifyJS2#compress-options

    – Rup
    Nov 15 '18 at 17:39

















Thank you. So it would be the same. Is there a way to keep the 'if'?

– Genji
Nov 15 '18 at 16:55







Thank you. So it would be the same. Is there a way to keep the 'if'?

– Genji
Nov 15 '18 at 16:55















If you can pass options through to uglifyjs, it's probably -c conditionals=false - github.com/mishoo/UglifyJS2#compress-options

– Rup
Nov 15 '18 at 17:39





If you can pass options through to uglifyjs, it's probably -c conditionals=false - github.com/mishoo/UglifyJS2#compress-options

– Rup
Nov 15 '18 at 17:39




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322290%2fjavascript-uglify-is-different-and-correct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python