Question about syntax


#1

https://www.codecademy.com/courses/learn-javascript/lessons/introduction-to-javascript/exercises/comments?action=lesson_resume

I don’t have any problems getting the code to run correctly. All three of my examples (A,B,C) run correctly. I’m just wondering if there is a preferred syntax? I don’t want to develop bad habits

```

A:

// opening line
console.log(‘It was love at first sight.’);

/* console.log(‘The first time Yossarian saw the chaplain he fell madly in love with him.’);
console.log(‘Yossarian was in the hospital with a pain in his liver that fell just short of being jaundice.’);
console.log(‘The doctors were puzzled by the fact that it wasn’t quite jaundice.’);
console.log(‘If it became jaundice they could treat it.’);
console.log(‘If it didn’t become jaundice and went away they could discharge him.’);
console.log(‘But this just being short of jaundice all the time confused them.’);
*/

B:
// opening line
console.log(‘It was love at first sight.’);
/*
console.log(‘The first time Yossarian saw the chaplain he fell madly in love with him.’);
console.log(‘Yossarian was in the hospital with a pain in his liver that fell just short of being jaundice.’);
console.log(‘The doctors were puzzled by the fact that it wasn’t quite jaundice.’);
console.log(‘If it became jaundice they could treat it.’);
console.log(‘If it didn’t become jaundice and went away they could discharge him.’);
console.log(‘But this just being short of jaundice all the time confused them.’);
*/

C:
// opening line
console.log(‘It was love at first sight.’);

/* console.log(‘The first time Yossarian saw the chaplain he fell madly in love with him.’);
console.log(‘Yossarian was in the hospital with a pain in his liver that fell just short of being jaundice.’);
console.log(‘The doctors were puzzled by the fact that it wasn’t quite jaundice.’);
console.log(‘If it became jaundice they could treat it.’);
console.log(‘If it didn’t become jaundice and went away they could discharge him.’);
console.log(‘But this just being short of jaundice all the time confused them.’); */

<do not remove the three backticks above>

#2

It really doesn’t matter how the comments are written. Everything after /* and before */ is ignored in any configuration. For readability (by humans), the preferred way is explicit…

/*
lines of comment text
*/

so the tokens stand out.

Aside

At this point in the learning curve there is not much need for efficiency or concern over CPU usuage. We only need to learn the syntax, reserved words and constructs at the elementary level. However, later on (once leaving these introductory courses) things like time complexity will become a concern.

One should know that console.log is notoriously slow and can have an impact on how fast our code will run (and on interactivity with the user). This becomes apparent when logging and getting user input at the same time in succession.

Not to stress it at this time, though, just to demonstrate how we can more efficiently log multiple lines of output…

let str;    // at start of program
// ...
str = [
  'It was love at first sight.',
  'The first time Yossarian saw the chaplain he fell madly in love with him.',
  'Yossarian was in the hospital with a pain in his liver that fell just short of being jaundice.',
  'The doctors were puzzled by the fact that it wasn’t quite jaundice.',
  'If it became jaundice they could treat it.',
  'If it didn’t become jaundice and went away they could discharge him.',
  'But this just being short of jaundice all the time confused them.'
].join('\n');

console.log(str);

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.