How Does Javascript if statement indentation work? [closed] - javascript

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 6 years ago.
Improve this question
How many spaces should be used for each line of if statement at the beginning of the line?
suppose we have 10 lines of nested if statements, what is the formula to determine how many lines to be used? Also can a programmer use the tab key instead to obtain proper indentation for a statement.
if (a !== 0) {
if (b > 1) {
if (c < 1) {

Indentations and spaces is not a requirement it just helps your code to be more readable.

Unlike languages like Python in which whitespace is considered semantic by the compiler, the Javascript compiler ignores runs of whitespace altogether. In my experience, though, the most common indentation (tab) lengths are 2 spaces and 4. My personal preference is 4, but plenty of other people will say 2.
I will say, though, that if you're using more than 4, you're either going to need to break your more nested logic up into multiple lines, or you're going to end up with a lot of overruns -- even with only 4 spaces, I often find myself running over 80 characters.

Related

How to use conditional (ternary) operator with nested if statements? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to add more nested "if statements" in the succeeding lines but I don't know what to do while using ternary operator.
Object.freeze (phoneBook)
function lookup(name, address) {
for (let i = 0; i < phoneBook.length; i++)
{
return (phonebook[i].givenName === name)? phoneBook[i][address]: "Not in the phonebook";
}
Ternary operations are hard to read if they are in any way complex. It rapidly becomes very difficult to work out what level you are at if there is much nesting.
Therefore for maintainability, especially if others are likely to have to support your code in the future, if then else statements properly laid out with indentation are probably a better way to go, even if you find yourself having to repeat return statements more than you would like.
Keep ternary statements for truly simple, one line, situations.

Should we start enclosing strings consistently using back ticks (`)? [closed]

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 3 years ago.
Improve this question
I am learning ES6. Just realized that using back ticks (`) to enclose string values has many benefits - and I am also sure it is little slower compare to simple string concatenation.
However, given the browser optimizations and advancement, is it safe to make it a general practice to enclose all strings using back ticks for consistency sake?
In other words, is it safe to establish it as a best practice to enclose string values with back ticks.
Back ticks are for template strings, if there are no variables for interpolation in your string then stick to a single quote.

How do you multiply negative numbers in javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This may sound stupid, but if you are trying to do something like 8*-5 (eight times negative five), you can't exactly.
In order of operations, you could do something like this: 8*(-5). But in javascript (and in most other languages), parentheses are the condition symbol.
Please help.
You can use both code in the javascript.
8*-5 == -40
8*(-5) == -40
As multiply symbol is greater precedence than minus symbol, it will be multiplied in normal math and do so in JavaScript.
if you are trying to do something like 8*-5 (eight times negative five), you can't exactly.
Of course you can. Open your JavaScript console and type 8*-5. You get -40.
In order of operations, you could do something like this: 8*(-5). But in javascript (and in most other languages), parentheses are the condition symbol.
Not sure what gave you that idea, but parenthesis can be used just fine in math operations and for setting order of operations. Again, open your JavaScript console, type 8*(-5) and you will see -40.

Parenthesis placing after function [closed]

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

Is there an IDE that highlights possible logical errors? If so, what is the best? [closed]

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 8 years ago.
Improve this question
For example, after spending an hour on the following javascript logic error, I finally realized the problem. Keep in mind that this if statement is embedded in a lot of other code.
for(i=0;i<alength;i++)
{
if(myvar = correct)
{
//ommitted irrelevent code
}
}
As you know, I was assigning the correct variable to myvar as well as comparing it, so it should have been:
if(myvar == correct)
After finally realizing the error, I was very annoyed that I didn't recognize it much faster. I have been programming for over 5 years, mostly in java, but this happens quite often and I was wondering if there is an IDE or something that can highlight possible logical errors.
You could have your code linted by JSHint.
There should be plugins for any major editors.
In this case, it would have told you "Expected a conditional expression and instead saw an assignment."

Categories