Javascript quiz 1

Can you answer these questions?

Miroslav Šlapka
3 min readDec 12, 2020

I get a few JS questions for you to broaden your Javascript horizons or perhaps just to refresh things you already know. Grab a pen and a piece of paper. Ok, the electronic form of it will do. Before you read the answers, try and write them down. Why bother?

As Israeli-American psychologist Daniel Kahneman said:

“A general “law of least effort” applies to cognitive as well as physical
exertion. The law asserts that if there are several ways of achieving the
same goal, people will eventually gravitate to the least demanding course
of action.
In the economy of action, effort is a cost, and the acquisition of skill is driven by the balance of benefits and costs. Laziness is built deep into our nature.

You could scroll straight to the answers since it’s easier, but imagine you are at a job interview or you are preparing for one. Sometimes we also use some JS features but can we really explain why and how they work? Well, I don’t :-) Let’s answer just 4 questions for this first JS quiz.

Questions:

  1. What is a difference between == and === ?
  2. What is an anonymous function? Pros and cons? What is IIFE?
  3. What are the ways of creating a JS object?
  4. How would you describe React?
Photo by Green Chameleon on Unsplash

Hey! You are jumping right here without trying to write it down?

Okay, I don’t blame you :-)

Answers:

1. What is a difference between == and === ?

In 1 word — coercion.
Coercion in JS means converting a value of one type to another. For example string to number. If we use == , type conversion happens before the comparison and comparing a string “2” with a number 2 would equal to true.

"2" == 2
> true

In contrary coercion isn’t allowed when the triple-equals === is used. It’s more strict so the previous example would look like:

"2" === 2
> false

In general, the === is considered a preferred option, but there are other edge cases worth to consider. You can read more about them here.

2. What is an anonymous function? Pros and cons? What is IIFE?

Anonymous function is a function expression that doesn’t have a name:

function() {…}

It is quicker and easier to write since it omits the name, but the drawbacks are prevalent:

  • difficult to debug — no name to display in a stack trace
  • anonymous function doesn’t tell us anything about its functionality that could be otherwise useful from the name (readability/maintenance)

In general it is best practice to name a function.

I described IIFE in this article about revealing module pattern.

3. What are the ways of creating a JS object?

The most common way is a literal syntax (or object initializer)

let car = { year: 2015}

We can also create a constructor function and initialize the object using new operator

function Person(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
var user = new Person("Peter", 24, "male");

Using the Object.create() method (it creates a new object, using an existing object as the prototype of the newly created object.)

Example taken from MDN docs:

const person = {
isHuman: false,
printIntroduction: function() {
alert(`My name is ${this.name}. Am I human ${this.isHuman}`);
}
};
const me = Object.create(person);me.name = 'Miro'; // property set on "me", but not on "person"
me.isHuman = true; // inherited properties can be overwritten
me.printIntroduction();
// expected output: "My name is Miro. Am I human? true"

4. How would you describe React?

I’ll leave this one for you. Let me know in the comments what you think about it.

Wrap up

Have you learned something new? Or was it a good refresher? I admit I forgot about Object.create() method while thinking about the objects and how to create them. And I learned something new about the edge cases of coercion comparison.

--

--