8
I always leave my css code and javascript minified but had never noticed the output generated. Well, I noticed that the last rule of a selector (in css) and the last function (in javascript) always loses the semicolon. For example, a simple rule in css:
div {
border: 2px solid red;
color: #fff;
}
Would be:
div{border:2px solid red;color:#fff} /*perdendo o último ponto e vírgula */
Another example, in javascript:
$(function(){
var foo = true;
if(foo) {
foo = false;
bar();
}
});
Will generate:
$(function(){var foo=true;if(foo){foo=false;bar()}}) /* sem os últimos ponto e vírgula */
In case there is only one function within a block, it also loses the score.
if(foo){
bar();
}
Vira:
if(foo){bar()}
My question on the subject is: Can the absence of these various semicolons influence the functioning of the code in any way? I did tests on simple codes but I’m wondering if I might have problems with more complex work.
I know that in the
css
, to the last rule, the existence of the;
is indifferent.– Cold
I believe it will depend a lot on the technology, some browsers with code legacy ("old") may not recognize, but for most users who should use IE, Safari or Chrome/Firefox make no difference.
– Olimon F.
The
;
is mere separator in CSS, has no reason to exist at the end. Actually are 2 questions in a, pq has nothing to do with the syntax of JS with that of CSS.– Bacco
http://answall.com/questions/3341/utilizar-ou-n%C3%A3o-ponto-e-v%C3%Adrgula-no-fim-das-linhas-no-javascript This topic answers your questions, it is a very complete answer
– Thallyson Dias
The question is excellent. So is the reply from bfavaretto. If you want to delve deeper into how Javascript’s role works and how to write good code for both maintainers and interpreters, I recommend reading the book Maintenable Javascript, by Nicholas C. Zakas. On the specific subject of the question, search for Automatic Semicolon Insertion - the mechanism by which Javascript analyzes and considers where an expression ends ;)
– Oralista de Sistemas