4 not so crucial things I didn't know about JavaScript
Tags: javascript, webdev
Published: 2020-08-01 16:33:36

---
title: 4 not so crucial things I didn't know about JavaScript
published: true
description: I wanted to put some not so crucial observations that I didn't know about JavaScript to blog post. Hope you will find them interesting.
tags: javascript, js, webdev
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/1nxtmgzqwa7ugl5ya3rm.jpg
---

There is so many things that you don't know about JavaScript. Some are crucial, some are not. I wanted to put some not so crucial observations that I didn't know about JavaScript to blog post. Hope you will find them interesting. Let's get to it.

## Just plain scope
Thing number one in this list is that you can use just plain { ... } scope whenever you want.
```js
let name = 'Big Lebowski';

{
	let nickname;
	if (name === 'Big Lebowski') {
		nickname = 'The Dude';
	} else {
		nickname = 'Walter';
	}
	// ...
}

console.log(nickname) // ReferenceError: nickname is not defined
```
This preserves the same functionality as other scopes with`if`, `for`, etc. But why would you use plain scope? Exactly why you are using lexical scoping in eg. `function` or `if` statements. To scope variables so they are not accessible from outer scope to prevent naming collision and keep things seperate. You can use these plain scopes to follow the [principle of least privilege](https://en.wikipedia.org/wiki/Principle_of_least_privilege). Not once did I come across upon such usage in any codebase that I run into but that doesn't mean it's useless.

## There is no `else if` in JS!
Wait, what?! I have used `else if` so many times and now it doesn't exist in JavaScript? The construction `else if` doesn't exists in the language AS IS. Only `if / else` exists. But how can I use `else if` then? Answer is easier that you might think. You just omit curly braces after `else`.

```js
if (a === b) {
	// ...
} else if (a === c) {
	// ...
}

// is actualy following but with omitted curly braces after else

if (a === b) {

} else {
	if (a === c) {
		// ...
	}
}
```
Omitting curly braces is not anything special in JavaScript. Recall this:
```js
const name = () => firstName + lastName;
```

Again, this is not anything crucial for your day to day life but just a fun fact.

## Naming loops and other statements! WHAT?!
You can label JavaScript statement to afterwards use `break` or `continue` with the label. 
```js
loop1:
for (let i = 0; i < 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (let j = 0; j < 3; j++) {   //The second for statement is labeled "loop2"
      if (i === 1 && j === 1) {
         break loop1;
      }
   }
}
```
You can also use them with those plain scopes I mentioned at the beginning of this post.
```js
someScope: {
	// ..
	break someScope
	// anything after break won't be executed
}
```
Again, I have not seen naming with label in any real codebase but that doesn't mean it doesn't have an use case for you in future.

## There is negative zero!
Yes, there is `-0` and it is valid.  Since JavaScript uses [IEEE 754 standard](https://en.wikipedia.org/wiki/IEEE_754) there are signed zeros (-0 and +0) and they are equal!
```js
-0 == 0 // true
-0 === 0 // true
```
You can still tell the difference with following code
```js
Object.is(-0, 0) // false
// or
1/0 === 1/-0; // false (1/0 === Infinity and 1/-0 === -Infinity)
```

Where negative zero occurs? In certain numeric operations such as
```js
0 / -3; // -0
0 * -3; // -0
```
Another case is using negative zero with vector logic where besides value you also needs to know the direction of the value.