This question already has answers here:
Do you recommend using semicolons after every statement in JavaScript?
(11 answers)
Closed 7 years ago.
In the book "Beginning JavaScript" by Jeremy McPeak and Paul Wilton there's a do-while loop example.
var userAge;
do {
userAge = prompt("Please enter your age","")
} while (isNaN(userAge) == true);
and below the author repeats one more time:
userAge = prompt ("Please enter your age","")
There's no semicolon at the end of the statement inside the do {} block. As far as I can remember the author stated that it's considered best to always end any statement with a semicolon although it's not a must in most cases.
Was the omission of ; intentional? It seems so considering that in both the 4th and 5th editions there's no semicolon.
And there's also a repetition of that line below which doesn't contain a semicolon either.
Of course, one might say that it's not even a mistake. What I want to understand is whether the omission of a semicolon is more likely to be a typo or (which is worse) rather was done intentionally. If the latter is true, isn't that a sign of inconsistency?
When writing or editing javascript, the semicolon helps a lot visually.
Depending on your editor, it may not be clear otherwise if you actually have a line break in there (ending the line of code). Instead, your editor may just be wrapping the line visually. So without the semicolon, you can make and overlook errors more easily. For more info:https://www.webmasterworld.com/forum91/521.htm
Related
This question already has answers here:
Negating specific characters in regex
(4 answers)
Reference - What does this regex mean?
(1 answer)
Closed 20 days ago.
I have a regular expression:
/'([^']*)'/
Am finding it hard to understand how it works. The function of the caret here confuses me.
Unlike this regex:
/[^01]/ : i understand the caret here is an inverter which means the search should return true for any input input value that is different from 01.
let quotedText = /'([^']*)'/;
console.log(quotedText.exec("She said 'hello'"));
The console: ["'hello'", "hello"]
I do understand how the regexpression(quotedText) finds hello. What if the statement was longer with more words in quote. Like:
("She said 'hello' and he responded 'Hi', 'do you need my help'").
Would the exec method find all the words or sentences in quotes?.
I am also very confused about the function of caret^ here. Is it inverting?? Or is it showing where the exec methods starts looking from. Whats the difference between [^']* and [^01]. Does the function of caret change based on the method. Does caret(^) you see work differently when used with test method or exec method?. does Caret behave differently when in square brackets?
This question already has an answer here:
List of reserved words in JavaScript
(1 answer)
Closed last year.
let type = "HI";
console.log(type)
I know its not a reserved word but its showing in blue color while taking as a variable so got a doubt?
Per this documentation, type is not reserved word for now or near future.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar
Note that typeof is reserved word. So, it is a matter of readability and naming conventions that you/your team may follow.
This question already has answers here:
JavaScript braces on new line or not? [closed]
(9 answers)
Closed 5 years ago.
I was used in this type of convention of indentation and curly brackets placement in college.
function code()
{
if(code)
{
code
}
}
but online tutorials in javascript tells me to do this style instead
function code(){
if(code){
code
}
}
this first one is also my preferred style because it is more clear and understandable. My question is why do I have to follow the second example? is there any advantages??
The official answer is that you can use either, but the practical answer is that it is safer to use the version where the opening curly brace is on the same line as the code block it defines.
In JavaScript, {} is the syntax for an object literal and JavaScript also has automatic semi-colon insertion. This automatic semi-colon insertion can cause functions written with the opening curly-brace on a different line than the function it defines the body of, to execute differently than you would expect. See this for details:
Why do results vary based on curly brace placement?
Mostly preference. I personally prefer the second way, but the only difference is when javascript's automatic semicolon insertion kicks in. There is no difference when dealing with function declarations or if statements.
This question already has answers here:
Javascript string comparison fails when comparing unicode characters
(5 answers)
Closed 8 years ago.
I have problem with condition of Germany charset:
if (jQuery(this).find("dt").text()=="Kälteleistung")
Its always return false but in jQuery(this).find("dt").text() alert print Kälteleistung
if(jQuery(this).find("dt").text()==jQuery("<div/>").html("Kälteleistung").text())
This simple solution help me. There are we are find special char of ä char. We are formed string for condition "Kälteleistung" and our task is return string which be same as we take in our html. We are need place it to html jQuery("<div/>").html("Kälteleistung") and take it .text(). Very simple and clear solution which does not require a third-party libraries.
Thanks!
I got your question working, without the need of extra stuff. I think your problem is in the selector not working good (there is no need to use find).
I have this sample, returning 'ok':
<div>Kälteleistung</div>
<script>
if ($("div").text()=="Kälteleistung")
{
alert('ok');
}
else
{
alert('not ok');
}
</script>
This question already has answers here:
What does the leading semicolon in JavaScript libraries do?
(6 answers)
Closed 9 years ago.
I was checking out the code of has.js and was puzzled by the initial semicolon here:
;(function(g){
// code
}()(this);
As far as I know, it does absolutely nothing. It does not put the function in expression position as () or ! do: (function(){}()) or !function(){}(). It seems to be merely a line ender for an empty line.
What is the purpose of this semicolon? An OCD desire for symmetry between the beginning and end of the IIFE? :)
It's there to prevent any previous code from executing your code as the arguments to a function.
i.e.
mybrokenfunction = function(){
} //no semicolon here
(function(g){
})(this);
will execute mybrokenfunction with your anonymous function as its argument:
mybrokenfunction = function(){}(function(g){})(this);
If you could guarantee that there won't be an unterminated (no semicolon) function before yours, you could omit the starting semicolon, but you can't, so it's just safer to put that extra semicolon in.