Skip to main content

JavaScript Specials Interview Questions

JavaScript Fundamentals: JavaScript Specials




How does JavaScript see, view, or treat line breaks?

View Answer:
Interview Response: JavaScript treats line breaks as delimiters, whitespace, and uses automatic semicolon insertion to close individual statements. Most code style guides agree that we should put a semicolon after each statement.

Code Example:

// automatic semicolon inserted by the JavaScript (V8) engine
// console.log('Hello')
// console.log('World'); <-- semicolon inserted

Can you describe the proper way to enforce strict mode in JavaScript?

View Answer:
Interview Response: To enable strict mode in JavaScript, add the directive "use strict" at the beginning of a script or function, which enables stricter parsing and error handling.

Technical Response: In JavaScript, To enforce strict mode, we must use the “use strict;” directive placed at the top of our code or function body. The directive must appear at the beginning of a script or at the start of a function body. Everything still works without "use strict", but some features behave in the old fashion, “compatible” way. We would generally prefer modern behavior.

Code Example:

Certainly! Here's how you would enforce strict mode in a JavaScript file or function:

For the entire script:

'use strict'; // Your first line of code starts here...

var x = 10;
console.log(x);

For a specific function:

function strictFunc() {
'use strict'; // Your first line of code in a function...
var y = 20;
console.log(y);
}

strictFunc();

In both examples, 'use strict'; helps to enforce better coding practices by making the JavaScript interpreter more strict.


What can a variable name include in JavaScript?

View Answer:
Interview Response: Variable names in JavaScript can include letters, digits, underscores, and dollar signs. They must start with a letter, underscore, or dollar sign, and are case-sensitive. Non-Latin alphabets and hieroglyphs are also permitted but rarely utilized.

Code Example:

Here are some examples of valid variable names in JavaScript.

var myVariable = 1;
let _anotherVariable = 2;
const $yetAnotherVariable = 3;
var Ω = 4; // Unicode variable names are allowed

And here's an example of an invalid variable name:

var 123abc; // this will cause an error, because variable names cannot start with a digit

Are JavaScript variables statically or dynamically typed?

View Answer:
Interview Response: Unlike statically typed languages, JavaScript variables are dynamically typed and do not require type declaration. This behavior in JavaScript means that variable data types in JavaScript are unknown at run-time.

Code Example:

let x = 5;
x = 'John';

What is the only operator with three parameters?

View Answer:
Interview Response: The only operator in JavaScript with three parameters is the conditional (ternary) operator (?:). It's a short way to write an if-else condition, formatted as condition ? exprIfTrue : exprIfFalse.

Code Example:

var age = 26;
var beverage = age >= 21 ? 'Beer' : 'Juice'; // ( ? ) conditional operator
console.log(beverage); // "Beer"

What three types of JavaScript functions are commonly used in application development?

View Answer:
Interview Response: JavaScript functions commonly used, in application development, include named functions, anonymous functions, and arrow functions, each serving different purposes and offering unique syntax.

Code Example:

// function declaration
function sum(a, b) {
let result = a + b;

return result;
}

// Function expression
let sum = function (a, b) {
let result = a + b;

return result;
};

// Arrow function
// expression at the right side
let sum = (a, b) => a + b;

// or multi-line syntax with { ... }, need return here:
let sum = (a, b) => {
// ...
return a + b;
};

// without arguments
let sayHi = () => console.log('Hello');

// with a single argument
let double = (n) => n * 2;