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.
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 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 an answer here:
Javascript RegEx Not Working [duplicate]
(1 answer)
Closed 5 years ago.
I tried the following JavaScript in developer console of Chrome:
s = "mysessionId=PsGymRfxWIQG9gjNGgRlKw"
s.match("mysessionId=([^\s\;]+)")
A little surprised by the result:
["mysessionId=P", "P"]
I had expected that the () in regexp will match the entire "PsGymRfxWIQG9gjNGgRlKw", instead, it only matched the first character "P".
When I tried the regexp in perl, it does match the entire sessionId.
Any idea why?
I should have used // instead of "".
s.match(/mysessionId=([^\s\;]+)/)
Had to laugh the moment when I figured it out.
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
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 7 years ago.
I'm using the following JavaScript code to create a regular expression to match a UK mobile number:
new RegExp("(\+44|0)7\d{9}", 'g');
However, I get an error in the console log saying:
Uncaught SyntaxError: Invalid regular expression: /(?:+44|0)7d{9}/:
Nothing to repeat
Similar questions on StackOverflow point to a missing escaped character, but mine seem to be fine.
I have also tried without the global flag.
Help would be greatly appreciated.
You can create RegExp by using short method
var a = /(\+44|0)7\d{9}/g
Its must works good)