Eloquent Javascript Chapter-1
Computer Language
Inside the computer’s world, there is only data, computer only understand the language of 0's and 1's. Data is stored in form of bits
For example, we can express the number 13 as thirteen but computer don't understand. Here are the bits that make up the number 13, with the weights of the digits shown below them:
0 0 0 0 1 1 0 1
128 64 32 16 8 4 2 1
Javascript Airthmetic Operations
- For Addition we use +
- For Substraction we use -
- For Division /
- For Multiplication *
- Modulo oerator to find the remainder %
Special Numbers
There are three special values in JavaScript that are considered numbers but don’t behave like normal numbers.
- NaN
- Infinity
Strings
Strings are used to represent text. They are written by enclosing their content in quotes. We can write a string in three different ways.
This is first way
'This is Second way'
"This is third way"
Majorly backticks is used widely, know why? because you can add anything in between by using this ${} which is called template literal.
Example
var userName = "Javascript"
var string = `Hello ${userName}`
Output
// Hello Javascript
Comparison
Here is one way to produce Boolean values:
console.log(3>2)
-> true
console.log(3<2)
->false
The > and < signs are the traditional symbols for “is greater than” and “is less than”, respectively. There are other similar operators such as >= (greater than equal to), <= (less than equal to)
Fact: 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
Not all operators are symbols. Some are written as words, one example is the typeof operator, which produces a string value naming the type of the value you give it.
console.log(typeof 4.5)
// → number
console.log(typeof "x")
// → string
Logical Operator
- && AND Operator The && operator represents logical and. It is a binary operator, and its result is true only if both the values given to it are true.
- || OR Operator The || operator denotes logical or. It produces true if either of the values given to it is true.
- ! Logical NOT Not is written as an exclamation mark (!). It is a unary operator that flips the value given to it—!true produces false, and !false gives true.
Ternary Operator
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy.
Syntax:
condition ? exprIfTrue : exprIfFalse
Type Coercion
Type coercion is the automatic or implicit conversion of values from one data type to another (such as strings to numbers). Type conversion is similar to type coercion because they both convert values from one data type to another with one key difference — type coercion is implicit whereas type conversion can be either implicit or explicit.
var value1 = '5';
var value2 = 9;
var sum = value1 + value2;
console.log(sum);
//Output - 59
In the above example, JavaScript has coerced the 9 from a number into a string and then concatenated the two values together, resulting in a string of 59. JavaScript had a choice between a string or a number and decided to use a string.
Top comments (0)