This site runs best with JavaScript enabled.

Some Javascript questions

Kevin Beck

July 22, 2019


I generally avoid this type of article but I some how eneded up reading it and a few of the quesitons I needed to look up, check I knew the right answer or could not explain very well. So after a bit of research (Google):

What does $ in JavaScript code refer to?

The '$' symbol is a valid JavaScript identifier.

JavaScript allows upper and lower case letters, numbers, $ and _. (apparently the $ was intended to be used for machine-generated variables such as $0001).

jQuery, and most javascript libraries use the $ as the primary base object (or function). Most of them also have a way to relinquish the $ so that it can be used with another library that uses it. In jQuery the $ is just a shortcut for jQuery.

Can you give an example of a curry function and why this syntax offers an advantage?

"Currying is a way of constructing functions that allows partial application of a function’s arguments. What this means is that you can pass all of the arguments a function is expecting and get the result, or pass a subset of those arguments and get a function back that’s waiting for the rest of the arguments."

var greetCurried = function(greeting) {
return function(name) {
console.log(greeting + ", " + name);
};
};
var greetHello = greetCurried("Hello");
greetHello("Heidi");
greetHello("Eddie");
greetCurried("Hi there")("Howard");

Why is it called a Ternary operator; what does the word Ternary indicate?

"The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement."

condition ? exprIfTrue : exprIfFalse

What's the difference between null and undefined?

In JavaScript, undefined means a variable has been declared but has not yet been assigned a value. null is an assignment value. It can be assigned to a variable as a representation of no value.

What is a closure?

A closure is a way of keeping access to variables in a function after that function has returned.

What's a typical use case for anonymous functions?

The most typical use for anonymous functions in Javascript is to pass them as arguments to other functions.

Describe the difference between localStorage, cookies, and sessionStorage.

"localStorage and sessionStorage are both so-called WebStorages and features of HTML5. localStorage stores information as long as the user does not delete them. sessionStorage stores information as long as the session goes. Usually until the user closes the tab/browser. Cookies are supported by older browsers and usually are a fallback for frameworks that use the above mentioned WebStorages. In contrast cookies can store way less information then WebStorages and the information."

What does a doctype do?

doctype is an integral part of HTML as defined in the specification:

"8.1.1 The DOCTYPE

A DOCTYPE is a required preamble.

DOCTYPEs are required for legacy reasons. When omitted, browsers tend to use a different rendering mode that is incompatible with some specifications. Including the DOCTYPE in a document ensures that the browser makes a best-effort attempt at following the relevant specifications."

Source