Values, Types, and Operators in JS Exposed

Raviprasanth
The Startup
Published in
5 min readDec 5, 2020

--

Javascript is a Programming Language that is used both on the client-side and server-side. Where HTML and CSS are languages that give structure and style to web pages, JavaScript gives web pages interactive elements that engage a user and that’s the reason, it is used everywhere.

This is blog is all about the first chapter in the book “The Eloquent Javascript” written by Marijn Haverbeke. This is a great book if u want to get into programming.

Values -

Data is stored inside the computer in the form of bits(i.e. 1s and 0s). A NonVolatile memory of a computer could save up to 30 billion bits and the volatile memory would have a few orders of magnitude more.

In order to work with such a huge number of bits, these bits have to be separated into chunks, to represent information. These chunks are termed as Values in Javascript Environment. These values could be Numbers, Text, Functions, etc. To create a value, simply invoke its name.

Numbers -

All Numeric values fall under the Number type. Javascript uses a 64-bit notation to store numeric values.

A few years back, computers had very low memory capacity and used 8 or 16-bit representation. As a result, the chances of memory overflow was high. Now, even the lowest spec mobile phone has a lot of memory. So, you don’t have to worry about overflow issues unless you are dealing with astronomical numbers.

We can store 2⁶⁴ different numbers in javascript which is equivalent to 18 quintillions. It is inclusive of negative numbers too. So, one bit is used to store the sign of the number.

But the actual issue is that the nonwhole numbers should also be stored within this range. So, the maximum whole number that could be saved will be in the range of 9 quadrillion which is still a lot.

Fractional numbers are represented using dot(.) and exponent could be represented using e.

Fractional Numbers => 23.45
Exponent => 283e3 (i.e.283*10^3)

Arithmetic -

All arithmetic operations like Addition, Subtraction, Multiplication, and Division could be done in javascript. The symbols that are used between numbers while performing arithmetic operations is called Operators.

If two or more operators lie on the same expression, they are processed on their order of precedence.

Bonus Tip #1

Wanna remember the order of precedence easily?!! Then you have to BODMAS. Sounds weird? Here’s what BODMAS means

B-Bracket

O-Order of

D-Division

M-Multiplication

A-Addition

S-Subtraction

Special Numbers -

There are three special numbers in javascript. They are infinity, -infinity and Nan. Using infinity based computations is not a best practice.

Nan stands for Not a number. Though it is a value of number type, you will get it in hypothetical computations like 0/0, infinity-infinity, etc.

String -

Sting is anything that represents text. You can use single quotes(‘), Double quotes(“) or Backticks(`) to represent String. The string within single quotes and double quotes have similar characteristics while Backticks has some additional features.

"This is a string using Double quotes"
'This is a string using Singlw quotes'
`This is a string using Backticks`

You cannot perform Arithmetic operations on strings but you can concatenate them using the ‘+’ operator.

"Hello" + "World" = "HelloWorld"

Bonus Tip#2

The string within Backticks is called Template literals. While using template literals, anything given with ${} will be computed, converted into a string, and placed in the same position.

`The sum of 5 and 6 is ${5+6}`
=> The sum of 5 and 6 is 11

Whenever there is a \ in a string, the character next to it is considered to have a special meaning. This is used to include special characters like ‘,”,`,\, etc into the string. These are called escape characters.

There are also several escape sequences available in Javascript to add Newline, form feed, Backspace, etc. To know more about this, check the below link.

Bonus Tip #3

While using Template literals, you don’t have to use escape sequence for the new line. Instead, you can just type it in a new line. For string quoted with single and double quotes, it will throw an error if string the string is defined in several lines.

var word=`this is line one
this is two and
this is three`;
console.log(word);
Output:
this is line one
this is two and
this is three

Unary and Binary Operators -

Operators which need only one operand to do computation are called Unary Operators. The typeof() is an example of a unary operator. It takes only one operand and returns the data type of it.

Operators which needs two operands to perform the computation are called Binary Operators. +,-,/,* are all examples of binary operators.

Boolean Values -

The boolean type has only two values. They are True and False. If the given condition is satisfied, it returns True else it returns False.

Relational operators like <, >, ==, ≤, ≥, and != are mostly used to perform this kind of boolean operations.

=>6<5
=>false

Bonus Tip #4

The only value which is not equal to itself is Nan. If you check the equivalence of Nan with Nan, it returns False.

=>(Nan==Nan)
=>False

Logical Operators -

Javascript supports three logical operators. They are AND(&&), OR(||) and NOT(!). It returns True or False based on the logical operator used.

console.log(true && false);
//false
console.log(false || true);
//true
console.log( !true );
//false

Bonus Tip #5

? is a Ternary operator. Any operator which needs more than two operands is called the Ternary operator.

? takes three operands, if the condition left to ? is true, the middle value will be returned. Else, the rightmost value is returned. The two values are separated by a colon (:).

console.log(true ? 1 : 2);
//1

Empty Values -

There are two empty values in Javascript. They are null and undefined. They are used to represent the absence of meaningful value. They are themselves values but don’t carry any information. Both null and undefined serve a similar purpose. So, their usage can be interchanged.

Conclusion

Have you ever accepted any challenge to complete an entire book in ten days and to write a blog about each chapter in that book?

This blog is one of such kind. This blog is so special for me that this is my first ever blog which I’m writing about the first-ever book I’ve read other than my curriculum. So Tanay Pratap sir, #teamtanayejschallenge accepted and I am super pumped to finish it.

To know more about this challenge, visit https://ejs-challenge.netlify.app/

--

--

Raviprasanth
The Startup
0 Followers

A Computer Science student who gets pumped up on learning new things