Summary
Compare with === and !==, never == and !=, which convert types first. In a condition, false, 0, an empty string, null, undefined and NaN count as false, and everything else as true. A cheat sheet of C# and its JavaScript equivalents.
Apprentice · JavaScript · Updated
After this lesson you can
- Compare values with === and !==, and predict which values are truthy and falsy
Key takeaways
- Compare with
===and!==, which work like C#'s==and!=.==converts types first, so5 == '5'istrue. false,0,'',null,undefinedandNaNare falsy. Everything else is truthy, including'0','false'and[].- When
0is a real answer, compare with===instead of checking whether the value is truthy. undefinedmeans nothing was ever given;nullis a value that's deliberately empty, like C#'snull.name ?? 'Guest'gives'Guest'whennameisnullorundefined, as in C#.
Two more differences catch out almost every C# developer. Then a cheat sheet to keep beside you.
=== not ==
JavaScript has two equality operators. == converts both values to the same type before comparing, which gives some strange answers. ===, strict equality, compares without converting: different types are never equal.
console.log(5 == '5');
console.log(5 === '5');
console.log(0 == '');
console.log(0 === '');
console.log(null == undefined);
console.log(null === undefined);
true
false
true
false
true
false
Always use === and !==. They behave like C#'s == and !=: 5 and '5' are different things. Forget == and != exist.
Truthy and falsy
In C#, an if needs a bool. In JavaScript, any value can go in a condition, and it counts as true or false. These values are falsy, counted as false (plus rarities such as -0):
| Falsy values |
|---|
false |
0 |
'' (an empty string) |
null |
undefined |
NaN |
Everything else is truthy, counted as true, including '0', 'false', empty arrays [] and empty objects {}.
const values = [0, 1, '', 'hello', null, undefined, [], 'false'];
for (const value of values) {
console.log(`${JSON.stringify(value)} is ${value ? 'truthy' : 'falsy'}`);
}
0 is falsy
1 is truthy
"" is falsy
"hello" is truthy
null is falsy
undefined is falsy
[] is truthy
"false" is truthy
It makes short checks easy, such as if (name) for "name has some text". But it's also why a count of 0 can be mistaken for "no count", so when 0 is a real answer, compare with ===.
null and undefined
JavaScript has two "no value" values:
undefined: nothing was ever given. A variable with no value, a missing argument, a property that isn't there, a function with noreturn.null: a value that's deliberately empty, set on purpose, like C#'snull.
Both are falsy, and ?? works like C#'s: name ?? 'Guest' gives 'Guest' when name is null or undefined.
Cheat sheet
| C# | JavaScript |
|---|---|
int x = 5; |
let x = 5; (or const) |
string s = |
const s = `Hi ${name}`; |
Console.WriteLine(x); |
console.log(x); |
int Add(int a, int b) { ... } |
function add(a, b) { ... } |
x => x * 2 |
x => x * 2 |
List<int> list = new List<int>(); |
const list = []; |
list.Add(7); / list.Count |
list.push(7); / list.length |
list.Where(...).Select(...) |
list.filter(...).map(...) |
new Person { Name = "Ada" } |
{ name: 'Ada' } |
a == b |
a === b |
null |
null, and undefined |
foreach (var item in list) |
for (const item of list) |
try { } catch (Exception ex) { } |
try { } catch (error) { } |
Learning more
The best reference for JavaScript is MDN, the Mozilla Developer Network: the JavaScript Guide for learning, and the JavaScript reference for looking things up. When you search the web for a JavaScript question, add mdn to the search.
Check yourself
1 question to try on the lesson page.
So what
Treat JavaScript as a new language that happens to look familiar. Compare with ===, know the falsy values, keep the cheat sheet handy, and the C# you know will carry you most of the way.