JavaScript: Logical expressions output
Look at the code below and try to guess what it will print.
console.log(0 || 1);
Correct answer::
1
The OR operator works in such a way that its execution (left to right) interrupts the first argument and returns the result, converting it into a value of true
.
Example:
console.log(0 && 1);
0
The AND operator works in such a way that its execution (left to right) interrupts the first argument and returns the result, converting it into a value of false
.
There are two basic rules in JavaScript by which conversions take place:
0
,''
,undefined
,NaN
, andnull
are converted intofalse
. Those values are considered falsy- Everything else is converted into
true
Developers use this extensively, for example, to define a default value:
const value = name || '';
// Examples
234 || ''; // 234
'hexlet' || ''; // 'hexlet'
undefined || ''; // ''
If name
is given one of the falsy values, an empty string will be assigned to the value
constant. In that case, in subsequent code, we will be able to treat value
as a string.
But there is a potential bug. If name
contains a falsy value and it's okay to assign values like 0
, undefined
, NaN
or null
to the value
constant, then the code above will get it wrong:
// Oops
false || ''; // ''
0 || ''; // ''
undefined || ''; // ''
We covered the comparison operators ===
and !==
in one of our lessons and we mentioned that JavaScript also has the operators ==
and !=
, though you shouldn't use them. The very difference lies in the type conversion:
console.log('' === false); // => false
console.log('' == false); // => true
An empty string and false
are different values, so the ===
operator says "False! They are not equal!".
But the ==
operator converts types, making an empty string and false
equivalent from its point of view.
This conversion is implicit, so avoid the ==
and !=
operators whenever possible.
Remember the negation operator:
const answer = true;
console.log(!answer); // => false
When you use double negation !!
the result value is equal to the initial value:
const answer = true;
console.log(!!answer); // => true
However, this is where type conversions also take place. So the result of the double negation will always be a boolean value. This trick sometimes helps change the data type.
Selection error
Imagine a task to check if a value is equal to either one thing or another. For instance, the variable value
must contain one of two values: first
or second
. Novice developers sometimes write this expression this way:
value === ('first' || 'second')
That's how we picture it in our minds, yet languages work differently, so this kind of code will lead to the wrong result. How do you read it correctly? We have to remember operator priority. The first thing to evaluate is wrapped in parentheses, i.e. 'first' || 'second'
. If we execute this code, the output will be:
node
'Welcome to Node.js v17.4.0.
> 'first' || 'second'
'first'
>
Now we can substitute the original expression with the partly evaluated one:
value === 'first'
Not what we expected at all. Now, back to the start. Let's write the test correctly:
// Parentheses are not necessary,
// because === precedence is higher than that of ||
value === 'first' || value === 'second'
Instructions
Write a function, getLetter()
, to extract a certain character (by ordinal number, not an index) from a given string and return it. If there is no such character, the function returns an empty string.
Examples:
const name = 'Hexlet';
// Such a call returns undefined
name[10]; // undefined
// The 11th char stands for the 10th index
getLetter(name, 11); // ''
getLetter(name, 1); // 'H'
getLetter(name, 0); // ''
getLetter(name, 6); // 't'
The exercise doesn't pass checking. What to do? 😶
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
My code is different from the teacher's one 🤔
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
I've read the lessons but nothing is clear 🙄
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.