Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am beginner in JavaScript and I am wondering why string literals with single and double quotes still live together? There already was a discussion on StackOverflow about which are better to use in certain situations, but why a majority of other languages do not have such feature, despite it is still an official JavaScript standard? It is much interesting to know - I do not believe this feature is some "rudimentary" stuff left from older versions. Thorough explanations are appreciated.
I am wondering why string literals with single and double quotes still live together?
I'm not following your "still" in that sentence. JavaScript was originally defined to have two kinds of quotes so that it's easier to do quoted quotes, and there's no reason to change that. There are lots of reasons not to change it. (Not least that it's useful. But also removing it would break a truly huge amount of code.)
...why a majority of other languages do not have such feature...
In most languages syntactically derived from B (as JavaScript is, like C, C++, C#, and Java), single quotes are used for character literals. But JavaScript doesn't have characters, just strings, so the single quotes aren't needed for that purpose.
...despite it is still an official JavaScript standard?
Yes, it's in the specification and that won't be changing.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am surprised that an answer to this is not easily found.
I am in the process of making a JSON schema. I have an object and one of it's properties is a string containing a regex pattern. This property must contain only regex.
So, this question is realistically two questions in one:
What is the regex pattern that describes regex patterns (javascript-compatible please)?
Secondly, how do I apply this to JSON schema (in the "pattern" property or even in the "patternProperty"1 property)?
1: I have no idea what purpose would require you to apply this in "patternProperty", but someone out there could find it useful
NOTE: Since, JSONschema is JSON and JSON is JavaScript-based, JavaScript scripters may find the solution (to the JSONschema-side of the question), as well as the problem, useful in their scripts.
Regexes can have nested parentheses.
Therefore, they are not describable by a regular expression.
Sorry.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
There are many ways to use JavaScript. When I use JavaScript with an anchor, I write code like this and I think this way is right.
Method One
But my co-worker uses JS like this.
Method Two
Is there a coding standard or are both methods correct?
DISCLAIMER: Inline JavaScript is, generally speaking, a bad idea, and 99% of the time you're much better off separating concerns, and using a library, such as jQuery, or whatever similar toolset that your framework of choice recommends.
Nonetheless, to answer your question, if you must use inline JavaScript, I recommend that you omit the "JavaScript:" keyword. It specifies a "pseudo-protocol," and is not necessary for modern browsers to interpret the code. It is a relic from the last decade, and there is a bug with some versions of IE:
"There is one (somewhat obscure) bug with the javascript protocol - in
Internet Explorer*, it will think you are leaving the page when you
click the link. If you are using window.onbeforeunload, then your
navigate-away message will appear at this time. For this reason alone,
we've stopped using the javascript protocol completely so we don't
have this bug show up because we forgot to check for it when we add a
navigate-away message to some page."
When do I need to specify the JavaScript protocol?
https://bytes.com/topic/javascript/answers/504856-javascript-pseudo-protocol-event-handlers
Both the ways are ok but in first way you should use a external JS file. Otherwise it is ok.
For small tasks and events second ways is good.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I was learning javascript from codecademy today.
There is one thing that confuse me a lot, in its tutorial, it use function names like orangeCost. like in this link:
http://www.codecademy.com/courses/javascript-beginner-en-6LzGd/1/1?curriculum_id=506324b3a7dffd00020bf661
In my behaviour, I feel both orange_cost and OrangeCost just fine.
Why in this broadly read tutorial, they use such variable name(orangeCost)?
Is there some history in it, or it can prevent some kind of catastrophe?
It's just a human convention.
In other environments, instead of using camel-casing, they use pascal-casing (orangeJuice would be camel-casing while OrangeJuice pascal-casing).
Conventions are powerful to let others understand our code as it's written in a standarized way.
It could happen that some convention would be ugly, but it's better to follow a convention than going alone the way.
Anyway, either camel or pascal casing aren't ugly per se. It's just our taste what turns something into ugly or beautiful.
An exception to the rule
Brainfuck code is ugly. I believe that there's a human convention about this ;)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Which is the common best practice for where to place parenthesis after a function? I see at times function () and I see function(). With parameters I see function (param) and then I see function(param. Is this just a matter of preference or is there a reason as to why there would be whitespace after the function or there would not be whitespace?
JavaScript is not white space sensitive.you define your coding style.
Though having white space between the function and parenthesis is no sin. If you follow crockford's javascript standards. He advises not to have space in between.
http://javascript.crockford.com/code.html#function
The size of the indent is usually independent of the style. Many early
programs used tab characters for indentation, for simplicity and to
save on source file size. Unix editors generally view tabs as
equivalent to eight characters, while Macintosh and Microsoft Windows
environments would set them to four, creating confusion when code was
transferred back and forth. Modern programming editors are now often
able to set arbitrary indentation sizes, and will insert the
appropriate combination of tabs and spaces. For Ruby, many shell
programming languages, and some forms of HTML formatting, two spaces
per indent level is generally used.
Read full on Code Indent Style in Programming
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I was checking the view source of the site http://mothereffinghsl.com/ . It was coded by Paul Irish
In the code the id of the html elements are not enclosed in quotes for few elements. For other elements the id is enclosed in quotes. Is this a good practice. Is it a mistake or purposefully omitted? I dont think a person like Paul has overlooked it.
<div id="main" role="main">
<h1>Mother-effing <span>hsl()</span></h1>
<canvas width=360 height=100>your browser doesnt support canvas.</canvas>
<span id=loading>one sec, bro.</span>
<label id=sat>Saturation</label>
What are your thoughts?
Edited after accepting the answer
Conclusion:
In html5 quotes are not necessary, but recommend for reasons mentioned in the comments.
Also its NOT a good practice to use quotes for some attributes and not for some other even though it is supported.
From the article Why attribute values should always be quoted in HTML
There are several reasons to use quotes around attribute values always:
It's easier, since you need not memorize and recall the rules for
allowable omission.
In XML, quotes are always required (since XML has
SHORTTAG NO; see Comparison of SGML and XML). You may use XML in the
future, and in that case your life will be simpler if you have
adopted the habit of quoting attribute values.
When someone (you or
someone else) later edits your HTML file, he may easily forget to add
the quotes if he edits an attribute value in manner which makes the
quotes mandatory. For example, an attribute like SRC=foo.gif is
legal, but if someone changes the attribute (e.g. due to moving a
file to another directory) to SRC=images/foo.gif it becomes illegal.
So, I'd say it's not the best of practices to omit quotes.
He's using HTML5. Attribute quotes are optional in HTML5 (and HTML 4, just not in XHTML). See Do you quote HTML5 attributes? and http://mathiasbynens.be/notes/unquoted-attribute-values (that second link references Paul Irish BTW!)