Skip to main content

JavaScript Specials

JavaScript Fundamentals: JavaScript Specials

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

View Answer:
Interview Response: JavaScript treats line breaks as delimiters 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
// alert('Hello')
// alert('World'); <-- semicolon inserted

Describe the proper way to enforce strict mode in JavaScript?

View Answer:
Interview Response: To enforce strict mode, we must use the “use strict;” directive placed at the top of our code or function body.

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:

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

What can a variable name include in JavaScript?

View Answer:
Interview Response: Variables can contain letters and numbers, but the first character must be a letter. In some cases, the characters $ and _ are regular and acceptable. Non-Latin alphabets and hieroglyphs are also permitted but rarely utilized.

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 (arguments)?

View Answer:
Interview Response: The only operator with three parameters is the ternary ( ? ) conditional operator.

Code Example:

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

Name three types of JavaScript functions commonly used in application development?

View Answer:
Interview Response: The three primary functions commonly used in JavaScript application development include a declaration, expression, and arrow functions.

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 = () => alert('Hello');

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