Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62
Subscribe to my email list now at http://jauyeung.net/subscribe/
To get a job as a JavaScript developer, front end or otherwise, we need to nail the coding interview.
In this article, we’ll look at some basic questions and the answers to them.
What’s the Difference Between undefined and null?
They both indicate a non-existent value, but we have to set null
explicitly.
On the other hand, undefined
is the value for undeclared variables, array positions that have been set with a value, and declared variables that haven’t been set with a value.
Also, null
has the type object and undefined
has type undefined. They’re both falsy and they are both primitive types.
The following will get us undefined
:
let x;
let y = undefined;
Also, array positions that haven’t been assigned a value will also be undefined
:
let arr = [];
arr[1];
arr[1]
is undefined
above.
Holes in arrays are also undefined
:
let arr = [,1];
arr[0]
They both convert to false
when cast to a boolean as follows:
Boolean(undefined);
Boolean(null);
!!undefined;
!!null;
null
equals undefined
when compared with the ==
operator, because they’re falsy. They aren’t equal when we compare them with the ===
operator since they’re of different types.
What Does the && Operator Do?
The &&
operator is the AND operator and it finds the first falsy expression and returns it. If there are no falsy expressions then it returns the last expression.
For example, if we have:
let foo = null && undefined && 'foo';
The foo
is null
since null
and undefined
are falsy.
If we have:
let foo = true && 'foo';
Then we have 'foo'
assigned to foo
. It does short-circuit evaluation to prevent unnecessary checks.
It’s also a handy shortcut for:
if (condition){
doSomething();
}
Because the code above is the same as:
condition && doSomething();
Because, if condition
is falsy then doSomething();
won’t run because of short-circuiting.
What Does the || Operator Do?
The ||
operator is the OR operator. It finds the first truthy expression and returns it.
It also uses short-circuiting to avoid unnecessary checks. Therefore, it’s handy for setting a default value of a variable in case the ones before the default choice are falsy.
For example, if we have:
let foo = null || undefined || 'foo';
Then we have 'foo'
as the value of foo
.
What Does the + Operator Do?
The +
operator has three meanings.
1. Unary +
If it’s put before an expression, it’ll try to convert it to a number. Putting an operator before an expression makes it a unary operator.
For example, if we want to get the UNIX timestamp for the current date-time, we can write:
+new Date();
+new Date(2020,1,1);
will return1580544000000
.
It’s a fast way to convert a string to a number also. For instance:
+'1'
This returns 1
.
Therefore, the unary +
operator tries to convert an expression after it. We can convert an expression to a number by wrapping it around parentheses as follows:
+(1 + '1');
Then we get 11
since 1 + ‘1’
returns '11'
.
If the expression can’t be converted to a number, it’ll return NaN
.
2. Addition operator
If the +
operator is between two numbers, it’ll add the two numbers.
For example, if we have:
1 + 2;
Then we get 3.
If we have:
let a = 1, b = 2;
a + b;
We also get 3.
3. Concatenation operator
If one or more operands in the expression aren’t numbers, it’ll try to convert it in different ways.
For example, if we have:
1 + '1'
Or:
'1' + 1
Then we get '11'
.
Of course, if we have two strings as operands then the +
operator will be used for string concatenation:
'foo' + 'bar';
That’ll return “foobar”
.
What’s the Difference Between ==
and ===
?
The difference between ==
and ===
is that ==
only checks the content for equality and ===
checks both the content and the data type for equality.
For example:
2 == '2'
This will return true
since they both have 2
as their content. This is because ==
does type coercion.
The full list of rules for type coercion for the ==
operator is:
- If
x
andy
have the same value, compare with the===
operator. - If
x
isnull
andy
isundefined
, returntrue
. - If
x
isundefined
andy
isnull
, returntrue
. - If
x
has thenumber
type andy
is a string, returnx == +y
. - If
x
has thestring
type andy
is anumber
, return+x == y
. - If
y
has theboolean
type, return+x == y
. - If
y
is aboolean
, returnx == +y
. - If
x
is astring
,symbol
, ornumber
andy
is anobject
then:
- Return
x == y.valueOf()
ify
is aString
instance. - Return
x == y.valueOf()
ify
is aNumber
instance. - Return
x == y[Symbol.toPrimitive]()
ify
is any other kind of object or value.
Conclusion
These are the most basic questions. To nail a JavaScript interview, these are the questions to start studying for.