Code Structure
JavaScript Fundamentals: Code Structure
Can you define what a statement is in JavaScript code structure?
View Answer:
Interview Response: Statements are syntax constructs and commands that perform actions. Usually, statements are written on separate lines to make the code more readable.
Technical Response: Statements are used in JavaScript to control the flow of the program. In contrast to properties, methods, and events fundamentally tied to the object that owns them, statements behave independently of any JavaScript object.
Code Example:
// This is a single statement
alert('Hello');
// This is a set of statements
alert('Hello');
alert('World');
// Set of statements on separate lines (recommended)
alert('Hello');
alert('World');
How are statements separated in JavaScript?
View Answer:
Interview Response: In JavaScript, we separate statements with a semi-colon. Although semi-colons are implicit in JavaScript, we should always explicitly add them to reduce the possibility of bugs in our code.
Code Example:
alert('Hello');
alert('JavaScript');
Can semi-colons be omitted in JavaScript?
View Answer:
Interview Response: Yes, but it is not recommended.
Technical Response: Yes, but it is not considered good code etiquette and should not be done. JavaScript interprets the line break as an “implicit” semi-colon, and this behavior is called an automatic semi-colon insertion.
Code Example:
// Missing semi-colon (;) (note the missing semi-colon)
// alert('Hello') <--
// correct implementation
alert('JavaScript');
Does JavaScript always interpret line breaks as the end of a statement?
View Answer:
Interview Response: There are cases when a newline does not mean a semi-colon and may result in an error.
Technical Response: : There are cases when a newline does not mean a semi-colon and may result in an error. The recommendation is to put semi-colons between statements even if newlines separate them. The JavaScript community widely adopts this rule.
Code Example:
// This will work…
alert(3 + 1 + 2);
// This will result in an error…
alert('There will be an error') // No semi-colon
[(1, 2)].forEach(alert); // results in an error
// But everything is fine again if we add a semicolon after alert:
alert('All fine now'); // uses a semi-colon alerts All fine now
[1, 2].forEach(alert); // alerts 1 and then 2
How are single-line comments created in JavaScript?
View Answer:
Interview Response: Single-line comments start with two forward slash characters //.
Code Example:
// This comment occupies a line of its own.
alert('Hello');
alert('JavaScript'); // This comment follows the statement
How are multi-line comments created in JavaScript?
View Answer:
Interview Response: Multiline comments start with a forward slash and an asterisk /* and end with an asterisk and a forward slash */.
Code Example:
/* An example with two messages.
This is a multiline comment.
*/
alert('Hello');
alert('JavaScript');
Are nested comments supported in JavaScript?
View Answer:
Interview Response: No, nested comments are not supported. It results in a syntax error.
Code Example:
/*
/* nested comment ?!? */
This is a comment continued <- returns a syntax error
*/ error
alert( 'Oh no, Mr. Bill...' );