DEV Community

Cover image for Values, Types, Operators in JavaScript - Eloquent JavaScript Chapter 1
Priyanshu
Priyanshu

Posted on • Edited on

Values, Types, Operators in JavaScript - Eloquent JavaScript Chapter 1

Reading has been fun but reading this book is something new experience as I know the basics of JavaScript and this book is making me revise all the concepts once again. I like it.
By the way I've completed the first chapter and here is the short summary blog from the first chapter.

I started reading it as there's a challenge for us which is called #teamtanayejschallenge where we have to read the book in ten days and write a summary blog for each chapter. As it's a summary blog I won't be including whole chapter here but give you an overall understanding.

Whatever we see on the internet, it's all data and it needs to be stored somewhere, so it's stored in Bits. Bits are small particles like atoms. It can be particles in your Compact Disk or Hard Disk. A modern computer has over 30 billion bits. If we start using them all at once, we can go out of memory and our program will elapse.

Now as you know bits are used to store data so to manage them well, we need to store them in small chunks.That's why we need to specify those bits in small chunks. Now in JavaScript too, we need to store data in small chunks and those chunks are called values.
Values can be numbers, text or functions (whatever can be useful). And to use them we have to name them, so we don't get lost in the crowd of values.

Numbers

Javascript uses fixed amount of bits to store values, and they use 64 of them to just store single numbers. THey are used to store everything related to the number like negative or positive, decimal values and if the number exceeds a certain amount, it starts adding an e for exponent.
Exponent means

2.998e8
2.998 × 10(to the power 8) = 299,800,000
Enter fullscreen mode Exit fullscreen mode

Now numbers contain arithmetics, it is also stored there for uses and can be used similarly as it's done in mathematics but the % symbol is a little different here. It gives you the remainder of two numbers for example (269 % 50 produces 19).
Here comes the special numbers like infinity and -infinity which renders as NaN. Also zero/zero renders as NaN. Here NaN means "Not a Number". It can also include any number which doesn't provide a meaningful result.

Strings

Strings are text written in backtick-quotes, single quotes or double quotes. They lie in a string until they end with the same kind of quote they started.
Single and double quoted texts can be used to concatenate which means:

"Pri" + "ya" + "nshu"
// Which will result in Priyanshu
Enter fullscreen mode Exit fullscreen mode

Now the backtick-quoted ones can do a little more and is often used. The same thing can be written like this.

Pri${ya}nshu
// This will also result in Priyanshu
Enter fullscreen mode Exit fullscreen mode

Unary Operators

Some operators are written as words which renders strings. For example typeof which outputs the type of the value you put to it.

Boolean Values

They are simple like "Yes" or "No". JavaScript has just two of them which is "true" or "false". Let's see some code.

let a = 9
let b = 7
// now here we will log two methods and see what it'll render
console.log(a<b)
console.log(a>b)
// Here JavaScript will render the first log as "false" and the second log as "true"
Enter fullscreen mode Exit fullscreen mode

There are also more similar comparisons (=, !=, ==, <=, >=), which I would like that you read it yourself in the book.

Empty Values

Whenever you try to get an output of a empty value, JavaScript knows that and renders "null" as result to let you know that there's no value and it's empty.

Automatic type conversion

Once you log a program. You expect result, so JavaScript tries to run all kinds of program that you wrote and want to execute. It checks for the code and returns whatever the result is. You can see the code below and see what it means.

console.log(8 * null)
// → 0
console.log("5" - 1)
// → 4
console.log("5" + 1)
// → 51
console.log("five" * 2)
// → NaN
console.log(false == 0)
// → true
Enter fullscreen mode Exit fullscreen mode

This code is copied from the book itself.
Whenever something goes wrong and you are missing on some code, you can check for the log (if you have logged it) and correct your mistakes.

Logical operators

Operators like && and || checks your code and renders the data whichever is true over there.

console.log(null || "user")
// → user
console.log("Agnes" || "user")
// → Agnes
Enter fullscreen mode Exit fullscreen mode

This code is copied from the book itself.

Final Words

This was all from the chapter one of the book.
All you read in this blog are basics, you can read more about them here.

If you liked the blog then "f" in the chat (comments) please.

Once again, it's for a blogging challenge where we have to read the book and write a blog for each chapter.
Go to the challenge web page.

I would like to hear from you about these topics. Also you can add something in comments if you want to share. For specific conversation you can tweet to me.

Top comments (0)