Code Structure - JavaScript Interview Questions
JavaScript Fundamentals: Code Structure
Can you define what a statement is in JavaScript code structure?
View Answer:
Interview Response: A statement in JavaScript is a single, standalone instruction that performs a specific action or computation within a program.
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
console.log('Hello');
// This is a set of statements
console.log('Hello');
console.log('World');
// Set of statements on separate lines (recommended)
console.log('Hello');
console.log('World');
What is the code structure of an Array in JavaScript?
View Answer:
Interview Response: In JavaScript, an array is defined using square brackets and consists of comma-separated values. Array elements can be of any data type.
Can semi-colons be omitted in JavaScript?
View Answer:
Interview Response: Semicolons can be omitted in JavaScript, but it is generally recommended to include them for better code readability and to avoid unexpected behaviors.
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)
// console.log('Hello') <--
// correct implementation
console.log('JavaScript');
What is the code structure of an Object in JavaScript?
View Answer:
Interview Response: In JavaScript, an object is defined using curly braces and consists of key-value pairs separated by commas. Methods can be added using functions as values.
Code Example:
let person = {
name: "John",
age: 30,
greet: function() {
console.log("Hello!");
}
};
Does JavaScript always interpret line breaks as the end of a statement?
View Answer:
Interview Response: No, JavaScript does not always interpret line breaks as the end of a statement. This is because JavaScript uses a semicolon (;) to indicate the end of a statement, and a line break is not always equivalent to a semicolon.
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…
console.log(3 + 1 + 2);
// This will result in an error…
console.log('There will be an error') // No semi-colon
[(1, 2)].forEach(console.log); // results in an error
But everything is fine again if we add a semicolon after console.log:
console.log('All fine now'); // uses a semi-colon console.logs All fine now
[1, 2].forEach(console.log); // console.logs 1 and then 2
What is the code structure of a Generator in JavaScript?
View Answer:
Interview Response: In JavaScript, a generator function is defined using the function asterisks syntax and yields values using the yield keyword within the function body.
Code Example:
function* numberGenerator() {
yield 1;
yield 2;
yield 3;
}
// Creating an instance of the generator
const generator = numberGenerator();
// Using the generator to produce values
console.log(generator.next().value); // Output: 1
console.log(generator.next().value); // Output: 2
console.log(generator.next().value); // Output: 3
console.log(generator.next().value); // Output: undefined
How are single-line comments created in JavaScript?
View Answer:
Interview Response: In JavaScript, single-line comments can be created by using the double forward slash (//) followed by the comment text. The text following the double forward slash will be ignored by the JavaScript interpreter and will not be executed.
Code Example:
// This comment occupies a line of its own.
console.log('Hello');
console.log('JavaScript'); // This comment follows the statement
What is the code structure of a function in javascript?
View Answer:
Interview Response: A JavaScript function consists of a function keyword, a function name, optional parameters enclosed in parentheses, and function code enclosed in curly braces.
Code Example:
Here's an example:
function greet(name) {
console.log("Hello, " + name + "!");
}
// Invoking the function
greet("John"); // Output: Hello, John!
In this example, greet
is a function that takes a parameter name
and logs a greeting message to the console. When the function is invoked with an argument ("John" in this case), it executes the code inside the function body, which produces the desired output.
Functions in JavaScript can also have a return statement to provide a value back to the caller. Here's an example:
function add(a, b) {
return a + b;
}
// Invoking the function and storing the result
let sum = add(5, 3);
console.log(sum); // Output: 8
In this case, the add
function takes two parameters a
and b
, performs the addition operation, and returns the result. The returned value is then assigned to the variable sum
and printed to the console.
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:
/*
This is a multi-line comment.
It can span across multiple lines.
It is commonly used for documenting code or temporarily disabling code blocks.
*/
console.log("Hello, 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
console.log( 'Oh no, Mr. Bill...' );