## DEV Community 👩‍💻👨‍💻 Pranish Shrestha

Posted on • Updated on • Originally published at dev.to

# Eloquent JavaScript (Part I: Chapter 1/values, types and Operators)

In this post, we will learn about:

Values
To be able to work with large quantities of bits without getting lost, we must separate them into chunks that represent pieces of information. In a JavaScript environment, those chunks are called values.Every value has a type that determines its role. Some values are numbers, some values are pieces of text, some values are functions, and so on.

Numbers
values of the number type are numeric values. for a number of 16, it will cause the bit pattern for the number 16 to come to existence.
JavaScript uses 64 bits to store a single values.

Arithmetic
Main thing to do with arithmetic is numbers
10+20*3
first the computer uses 20*3 and then it adds with the rest. to make the addition first we use parenthesis.
(10+20)3 //now first adds then multiply
the computer solves in this order /
+- .i.e. division, multiplication, addition and then subtraction.
There's also another one i.e. %.

Special Numbers
There are only three special values in JavaScript.
first two are infinty and - infinity and the 3rd one is NaN which stands for not a number.
if you try to calculate 0/0 or infinity - infinity etc. , you'll get NaN.

Strings
anything inside quotes are strings.

``````"hello this is a string"

"hello \n this is a string"
Output:
hello
this is a string
``````

/t is a tabline character

``````console.log(`half of 100 is \${100 / 2}`)
output: half of 100 is 50
``````

Unary Operators
Operators that use one values are called unary operators.

``````console.log(typeof 4.5)
// → number
console.log(typeof "x")
// → string
``````

operators that use two values are called binary operators
example:

``````console.log(- (10 - 2))
// → -8
``````

Boolean Value
JavaScript produce two boolean values : true and false

Comparison

``````console.log(3 > 2)
// → true
console.log(3 < 2)
// → false
``````

The way strings are ordered in roughly alphabetic.When comparing strings, JavaScript goes over the characters from left to right, comparing the Unicode codes one by one.

• "a"<"b"<"c"<"d" etc.
• "A"<"a"

There is only one value in JavaScript that is not equal to itself, and that is NaN (“not a number”).

``````console.log(NaN == NaN)
// → false
``````

==,!=,<,>,<=,>= are used.

Logical Operators
JavaScript supports three logical operators: and , or , not
And(&&), OR(||), not(!)
Example:

``````console.log(true&&false) //false
console.log(true||false) //true
``````

ternary operator:

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

Empty values
There are two special valus: null and undefined The difference in meaning between undefined and null is an accident of JavaScript’s design, and it doesn’t matter most of the time. In cases where you actually have to concern yourself with these values, I recommend treating them as mostly interchangeable.

Automatic Type Conversion

``````console.log(8 * null) //null becomes zero
// → 0
console.log("5" - 1)
// → 4
console.log("5" + 1) //concatenate
// → 51
console.log("five" * 2) //string and a number results Not a number(Nan)
// → NaN
console.log(false == 0)
// → true
``````

When an operator is applied to the “wrong” type of value, JavaScript will quietly convert that value to the type it needs, using a set of rules that often aren’t what you want or expect. This is called type coercion

I recommend using the three-character comparison operators defensively to prevent unexpected type conversions from tripping you up. But when you’re certain the types on both sides will be the same, there is no problem with using the shorter operators.

Short-circuiting of logical operators

The logical operators && and || handle values of different types in a peculiar way. They will convert the value on their left side to Boolean type in order to decide what to do, but depending on the operator and the result of that conversion, they will return either the original left-hand value or the right-hand value.

example:

``````console.log(null || "user")
// → user
console.log("Agnes" || "user")
// → Agnes
``````

If the initial value can be converted to false, you’ll get the replacement instead. The rules for converting strings and numbers to Boolean values state that 0, NaN, and the empty string ("") count as false, while all the other values count as true. So 0 || -1 produces -1, and "" || "!?" yields "!?".

The && operator works similarly but the other way around. When the value to its left is something that converts to false, it returns that value, and otherwise it returns the value on its right.

Another important property of these two operators is that the part to their right is evaluated only when necessary. In the case of true || X, no matter what X is—even if it’s a piece of program that does something terrible—the result will be true, and X is never evaluated. The same goes for false && X, which is false and will ignore X. This is called short-circuit evaluation.

Conclusion ⌛
I hope you found these tips helpful. If you need any help please let me know in the comment section.

👋 Thanks for reading, See you next time