# Nonzero Numbers explainer

Aim of the puzzle: Introduce the concept of Truthy and Falsy
Walk through of solution: The test of an if statement is always either `true` or `false`. For example:

``````let x = 3
if (x === 3) {
console.log('true!')
}
``````

This code will print `'true!'`because `x` is equal to `3`.

However, what if we write it like this?

``````let x = 3
if (x) {
console.log('true!')
}
``````

This code will also print `'true!'`. Weird!

This code works because every value in JavaScript is either “truthy” or “falsy”.

It seems confusing, but it’s a bit simpler than it sounds.

There are only 6 falsy values in JavaScript. Everything else is truthy. The 6 falsy values are:

1. `false`: the boolean value `false`
2. `0`: the number `0`
3. `''`: An empty string
4. `NaN`: which means “Not a Number”, and is typically caused by math errors
5. `undefined`: a variable’s value before it has been assigned a value
6. `null`: a blank value that can be assigned to a variable

In this puzzle, a classic for loop is set up with `i` initially set at `0 - 3` (this is a workaround for most mobile phone keyboards not allowing users to input negative numbers). It will loop as long as `i < 4`, and `i` will increment by `1` with each iteration.

Inside the for loop, an if statement tests `i`. If `i` has a truthy value, the `console.log()` will print `i`. If `i` has a falsy value, it won’t be printed.

When the code runs, every number from `-3` to `3` except `0` will be printed to the console.
Sample code solution:

``````for (let i = 0 - 3; i < 4; i++) {
if (i) {
console.log(i);
}
}
``````

Javascript Concepts: Truthy and Falsy, If Statements, Booleans, `console.log()`, For Loops

1 Like

I need help!!! I dont know what i am doing wrong

Happy to help! Post a screenshot of your code and I can take a look.

Hey there, try putting an if statement inside the for loop like this:

``````for (let i = 0 - 3; i < 4; i++) {
if (i) {
console.log(i);
}
}
``````

If statements test if something is true or false. In this puzzle, we’re learning about truthiness and falsiness, and showing that `0` is one of the six falsy values.

Hope this helps! Let me know if you have any questions.
Ben