0

Added Later: I want to add further clarification on my concern. My concern is un-minified code without semicolon works. But what about absence of semicolon in minified code (which is in single line)? because it indicates END of the statement.

I have also gone through the answers of this question https://stackoverflow.com/questions/5840845/reliable-and-convenient-javascript-minifier#=

I have also gone through stephenwlther link.

But still my concern is not resolved. Because minifier tools removes semicolons from lot of lines.

Check this un-minified code...

(jQuery)(function ($) {

function statsCount(options) {
    var $this = $(this);
    options = $.extend({}, options || {}, $this.data('countToOptions') ||        {});
    $this.countTo(options);
} // NUMBERS COUNTER END

function statsAnimate() {
    // NUMBERS COUNTER START
    $('.numbers').data('countToOptions', {
        formatter: function (value, options) {
            return value.toFixed(options.decimals).replace(/\B(?=(?:\d{3})+(?!\d))/g, ',');
        }
    });
    // start timer
    $('.timer').each(statsCount);
}

if (typeof (CMA) != "undefined" && CMA.elementsAnimation) {
    $('.numbers-counter').waypoint(function () {
        statsAnimate();
    },
            {offset: '70%'}
    );
} else {
    statsAnimate();
}
});

minifier.org giving me below code....

(jQuery)(function($){function statsCount(options){var $this=$(this);options=$.extend({},options||{},$this.data('countToOptions')||});$this.countTo(options)}function statsAnimate()$('.numbers').data('countToOptions',formatter:function(value,options){return value.toFixed(options.decimals).replace(/\B(?=(?:\d{3})+(?!\d))/g,',')}});$('.timer').each(statsCount)}if(typeof(CMA)!="undefined"&&CMA.elementsAnimation){$('.numbers-counter').waypoint(function()statsAnimate()},offset:'70%'})}else{statsAnimate()}})

Note the absence of semicolon at the end and from lot of places.

If this correct??

Community
  • 1
  • 1
NehaAgra
  • 39
  • 1
  • 9
  • 1
    Is the minified code not working? Semicolons are optional in javascript. Minifiers try to decrease as many bytes as they can without breaking your code – Mukesh Soni May 21 '17 at 12:42
  • 1
    Semicolons are **not** optional in JavaScript: the way you put it, it seems that one can remove all semicolons. The thing is that *some* semicolons are optional, some are unnecessary and some are even wrong. – Gerardo Furtado May 21 '17 at 12:45
  • @MukeshSoni i am worried to test whether minified code is working or not as i am working on live and high-traffic news portal site. That is why i decided to confirm. – NehaAgra May 21 '17 at 12:46
  • 2
    I hope you have a test environment to test your code before it goes to production. @gerardo - javascript engine does automatic semicolon insertion for you. But minifiers remove semicolon in a way that it will not result in wrong interpretation as opposed to code with semicolon – Mukesh Soni May 21 '17 at 12:47
  • 1
    @MukeshSoni try a for loop without semicolons separating the expressions.., what I'm saying is that, for someone that doesn't know JS that much (which may be OP's case), your statement might make him/her think that *all* semicolons are optional. You could say: *"semicolons as **statement terminators** are optional"*. – Gerardo Furtado May 21 '17 at 12:50
  • Can you give a jsfiddle or jsbin link for the for loop example? – Mukesh Soni May 21 '17 at 12:56
  • @GerardoFurtado i believe i did not precisely express my concern. So added few more lines in question. – NehaAgra May 21 '17 at 13:05
  • You can still try your code locally without going to the production server. – krillgar May 21 '17 at 13:06
  • Does this answer your question? [Where do I add semi-colons in JS to prevent minification errors?](https://stackoverflow.com/questions/22812066/where-do-i-add-semi-colons-in-js-to-prevent-minification-errors) – Michael Freidgeim Feb 17 '23 at 23:57

0 Answers0