0

as the title have said,

When do stuff like,

var i;
    i = 0
      for (i = 0; i < 100; i++) {
          $('<p>'+[i]+' :, </p>').appendTo('#textbox')
        }

Does Integer 01 mean the same as 1? I am doing this to make my code look better.

00 :, 01 :, 02 :, 03 :, 04 :, 05 :, 06 :, 07 :, 08 :, 09 :, 
10 :, 11 :, 12 :, 13 :, 14 :, 15 :, 16 :, 17 :, 18 :, 19 :, 
20 :, 21 :, 22 :, 23 :, 24 :, 25 :, 26 :, 27 :, 28 :, 29 :, 
30 :, 31 :, 32 :, 33 :, 34 :, 35 :, 36 :, 37 :, 38 :, 39 :, 
40 :, 41 :, 42 :, 43 :, 44 :, 45 :, 46 :, 47 :, 48 :, 49 :, 
50 :, 51 :, 52 :, 53 :, 54 :, 55 :, 56 :, 57 :, 58 :, 59 :, 
60 :, 61 :, 62 :, 63 :, 64 :, 65 :, 66 :, 67 :, 68 :, 69 :, 
70 :, 71 :, 72 :, 73 :, 74 :, 75 :, 76 :, 77 :, 78 :, 79 :, 
80 :, 81 :, 82 :, 83 :, 84 :, 85 :, 86 :, 87 :, 88 :, 89 :, 
90 :, 91 :, 92 :, 93 :, 94 :, 95 :, 96 :, 97 :, 98 :, 99 :, 

Better than this, right?

0 :, 1 :, 2 :, 3 :, 4 :, 5 :, 6 :, 7 :, 8 :, 9 :, 
10 :, 11 :, 12 :, 13 :, 14 :, 15 :, 16 :, 17 :, 18 :, 19 :, 
20 :, 21 :, 22 :, 23 :, 24 :, 25 :, 26 :, 27 :, 28 :, 29 :, 
30 :, 31 :, 32 :, 33 :, 34 :, 35 :, 36 :, 37 :, 38 :, 39 :, 
40 :, 41 :, 42 :, 43 :, 44 :, 45 :, 46 :, 47 :, 48 :, 49 :, 
50 :, 51 :, 52 :, 53 :, 54 :, 55 :, 56 :, 57 :, 58 :, 59 :, 
60 :, 61 :, 62 :, 63 :, 64 :, 65 :, 66 :, 67 :, 68 :, 69 :, 
70 :, 71 :, 72 :, 73 :, 74 :, 75 :, 76 :, 77 :, 78 :, 79 :, 
80 :, 81 :, 82 :, 83 :, 84 :, 85 :, 86 :, 87 :, 88 :, 89 :, 
90 :, 91 :, 92 :, 93 :, 94 :, 95 :, 96 :, 97 :, 98 :, 99 :, 

Will statement like this,

if(something == 01){Do Something}

Work same as this,

if(something == 1){Do Something}

Without actually change all those 1 into 01? Does integer of 01 mean the same as 1, or are they two different integers.

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
  • 1
    if `something` is equal to `1` or `01` both if statements will work. Basically 01 and 1 is the same and will act the same if it's an integer. If you run `console.log(01)` in your browser you will see it returns `1` – Carsten Løvbo Andersen Nov 06 '20 at 07:08
  • 1
    Yes both are same if it is integer. – Yuvraj Mule Nov 06 '20 at 07:12
  • 2
    They're not exactly the same. Some versions of JS / ES support octal literals prefixed with 0, see e.g. https://stackoverflow.com/questions/34358331/why-are-octal-numeric-literals-not-allowed-in-strict-mode-and-what-is-the-worka – Jonas Høgh Nov 06 '20 at 07:27

2 Answers2

1

The integers 01 and 1 are the same. I tested if 0 could be a prefix for octal number, but it is not (It is 0o, eg. 0o73 is decimal 59).

miny1997
  • 469
  • 3
  • 13
  • This is only correct in modern ES versions. 0-prefixed octal literals were previously supported, and their use is not recommended. – Jonas Høgh Nov 06 '20 at 08:23
1

If you want your #textbox to look nice, you could instead try using padStart():

for (var i = 0; i < 100; i++) {
    $('<p>'+[i.toString().padStart(2,'0')]+' :, </p>').appendTo('#textbox')
}

If you're wanting some section of your JavaScript code to to look cleaner, you could simply indent the first row like so:

 0 :,  1 :,  2 :,  3 :,  4 :,  5 :,  6 :,  7 :,  8 :,  9 :, 
10 :, 11 :, 12 :, 13 :, 14 :, 15 :, 16 :, 17 :, 18 :, 19 :, 
20 :, 21 :, 22 :, 23 :, 24 :, 25 :, 26 :, 27 :, 28 :, 29 :, 
30 :, 31 :, 32 :, 33 :, 34 :, 35 :, 36 :, 37 :, 38 :, 39 :, 
40 :, 41 :, 42 :, 43 :, 44 :, 45 :, 46 :, 47 :, 48 :, 49 :, 
50 :, 51 :, 52 :, 53 :, 54 :, 55 :, 56 :, 57 :, 58 :, 59 :, 
60 :, 61 :, 62 :, 63 :, 64 :, 65 :, 66 :, 67 :, 68 :, 69 :, 
70 :, 71 :, 72 :, 73 :, 74 :, 75 :, 76 :, 77 :, 78 :, 79 :, 
80 :, 81 :, 82 :, 83 :, 84 :, 85 :, 86 :, 87 :, 88 :, 89 :, 
90 :, 91 :, 92 :, 93 :, 94 :, 95 :, 96 :, 97 :, 98 :, 99 :, 

The above would be fine assuming some kind of formatter or beautifier wasn't messing with things.

Alex
  • 310
  • 1
  • 6