Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions 02.Fundamentals/07.typeConversions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
///// String Conversion
let value = true;
console.log(typeof value); // boolean

value = String(value); // now value is a string

console.log(typeof value); // string

// String conversion is mostly obvious. A false becomes "false", null becomes "null", etc.



///// Numeric Conversion

let string = "123";
console.log(typeof string); // string

let num = Number(string); // becomes a number 123

console.log(typeof num); // number


// If the string is not a valid number, the result of such a conversion is NaN. For instance:

let age = Number("an arbitrary string instead of a number");

console.log(age); // NaN, conversion failed


///// Boolean Conversion


console.log(Boolean(1)); // true
console.log(Boolean(0)); // false

console.log(Boolean("hello")); // true
console.log(Boolean("")); // false






216 changes: 216 additions & 0 deletions 02.Fundamentals/08.basicOperators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
We know many operators from school.They are things like addition +, multiplication *, subtraction -, and so on.


In this chapter, we’ll start with simple operators, then concentrate on JavaScript - specific aspects, not covered by school arithmetic.

Terms: “unary”, “binary”, “operand”
*/


// An operand – is what operators are applied to. For instance, in the multiplication of 5 * 2 there are two operands: the left operand is 5 and the right operand is 2. Sometimes, people call these “arguments” instead of “operands”.


// An operator is unary if it has a single operand. For example, the unary negation - reverses the sign of a number:


let value = 1;
value = -value;
console.log(value); // -1, unary negation was applied



// An operator is binary if it has two operands. The same minus exists in binary form as well:

let x = 1, y = 3;
console.log(y - x); // 2, binary minus subtracts values


///// Maths

/*
The following math operations are supported:

1.Addition +,
2.Subtraction -,
3.Multiplication *,
4.Division /,
5.Remainder %,
6.Exponentiation **.


The first four are straightforward, while % and ** need a few words about them.

*/

///// Remainder %

console.log(5 % 2); // 1, the remainder of 5 divided by 2
console.log(8 % 3); // 2, the remainder of 8 divided by 3
console.log(8 % 4); // 0, the remainder of 8 divided by 4



///// Exponentiation **

console.log(2 ** 2); // 2² = 4
console.log(2 ** 3); // 2³ = 8
console.log(2 ** 4); // 2⁴ = 16


///// String concatenation with binary +

let text = "my" + "string";
console.log(text); // Output : mystring

// Note that if any of the operands is a string, then the other one is converted to a string too.

console.log('1' + 2); // "12"
console.log(2 + '1'); // "21"


console.log(2 + 2 + '1'); // "41" and not "221"

console.log('1' + 2 + 2); // "122" and not "14"


///// Numeric conversion, unary +

// No effect on numbers
let num1 = 1;
console.log(+num1); // 1

let num2 = -2;
console.log(+num2); // -2

// Converts non-numbers - It actually does the same thing as Number(...), but is shorter.

console.log(+true); // 1
console.log(+""); // 0


let apples = "2";
let oranges = "3";

console.log(apples + oranges); // "23", the binary plus concatenates strings

// both values converted to numbers before the binary plus
console.log(+apples + +oranges); // 5

// the longer variant
// console.log( Number(apples) + Number(oranges) ); // 5


///// Assignment

let val = 2 * 2 + 1;

console.log(val); // 5

// Assignment = returns a value

let a = 1;
let b = 2;

let c = 3 - (a = b + 1);

console.log(a); // 3
console.log(c); // 0

// Chaining Assignments

let n1, n2, n3;

n1 = n2 = n3 = 2 + 2;

console.log(n1); // 4
console.log(n2); // 4
console.log(n3); // 4


// Modify-in-place

// We often need to apply an operator to a variable and store the new result in that same variable.

let nx = 2;
nx = nx + 5;
nx = nx * 2;

// Th3 above notation can be shortened using the operators += and *=:

let n = 2;
n += 5; // now n = 7 (same as n = n + 5)
n *= 2; // now n = 14 (same as n = n * 2)

console.log(n); // 14

// Short “modify-and-assign” operators exist for all arithmetical and bitwise operators: /=, -=, etc.


///// Increment/decrement

// Increment ++ increases a variable by 1:
let counter = 2;
counter++; // works the same as counter = counter + 1, but is shorter
console.log(counter); // 3

// Decrement -- decreases a variable by 1:
counter--; // works the same as counter = counter - 1, but is shorter
console.log(counter); // 1

/*
Increment/decrement can only be applied to variables. Trying to use it on a value like 5++ will give an error.

The operators ++ and -- can be placed either before or after a variable.

When the operator goes after the variable, it is in “postfix form”: counter++.

The “prefix form” is when the operator goes before the variable: ++counter.

*/
// If we’d like to increase a value and immediately use the result of the operator, we need the prefix form:


let count = 0;

// If we’d like to increase a value and immediately use the result of the operator, we need the prefix form:
console.log(++counter); // 1


// If we’d like to increment a value but use its previous value, we need the postfix form:
console.log(counter++); // 0


///// Bitwise operators

/*
Bitwise operators treat arguments as 32-bit integer numbers and work on the level of their binary representation.

These operators are not JavaScript-specific. They are supported in most programming languages.


The list of operators:

- AND ( & )
- OR ( | )
- XOR ( ^ )
- NOT ( ~ )
- LEFT SHIFT ( << )
- RIGHT SHIFT ( >> )
- ZERO-FILL RIGHT SHIFT ( >>> )

*/


///// Comma

// The comma operator , is one of the rarest and most unusual operators. Sometimes, it’s used to write shorter code, so we need to know it in order to understand what’s going on.

let val_xyz = (1 + 2, 3 + 4);
console.log(val_xyz); // 7 (the result of 3 + 4)


// three operations in one line
for (a = 1, b = 3, c = a * b; a < 10; a++) {
/// Piece of code here
}
101 changes: 101 additions & 0 deletions 02.Fundamentals/09.comparison.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
We know many comparison operators from maths.

In JavaScript they are written like this:

- Greater/less than: a > b, a < b.
- Greater/less than or equals: a >= b, a <= b.
- Equals: a == b, please note the double equality sign == means the equality test, while a single one a = b means an assignment.
- Not equals: In maths the notation is ≠, but in JavaScript it’s written as a != b.

Boolean is the result
All comparison operators return a boolean value:

- true – means “yes”, “correct” or “the truth”.
- false – means “no”, “wrong” or “not the truth”.
*/

console.log(2 > 1); // true (correct)
console.log(2 == 1); // false (wrong)
console.log(2 != 1); // true (correct)

// A comparison result can be assigned to a variable, just like any value:

let result = 5 > 4; // assign the result of the comparison
console.log(result); // true

///// String comparison

// To see whether a string is greater than another, JavaScript uses the so-called “dictionary” or “lexicographical” order.

// In other words, strings are compared letter-by-letter

/*
The algorithm to compare two strings is simple:

1. Compare the first character of both strings.
2. If the first character from the first string is greater (or less) than the other string’s, then the first string is greater (or less) than the second. We’re done.
3. Otherwise, if both strings’ first characters are the same, compare the second characters the same way.
4. Repeat until the end of either string.
5. If both strings end at the same length, then they are equal. Otherwise, the longer string is greater.

*/

console.log('Z' > 'A'); // true
console.log('Glow' > 'Glee'); // true
console.log('Bee' > 'Be'); // true
/*

In the first example above, the comparison 'Z' > 'A' gets to a result at the first step.

The second comparison 'Glow' and 'Glee' needs more steps as strings are compared character-by-character:

G is the same as G.
l is the same as l.
o is greater than e. Stop here. The first string is greater.

*/


///// Comparison of different types

console.log('2' > 1); // true, string '2' becomes a number 2
console.log('01' == 1); // true, string '01' becomes a number 1

///// Strict equality

// A regular equality check == has a problem. It cannot differentiate 0 from false:

console.log(0 == false); // true
console.log('' == false); // true

// A strict equality operator === checks the equality without type conversion.

console.log(0 === false); // false, because the types are different


///// Comparison with null and undefined

// For a strict equality check ===
console.log(null === undefined); // false

// For a non-strict check ==
console.log(null == undefined); // true


// For maths and other comparisons < > <= >=
console.log(null > 0); // (1) false
console.log(null == 0); // (2) false
console.log(null >= 0); // (3) true

// An incomparable undefined
console.log(undefined > 0); // false (1)
console.log(undefined < 0); // false (2)
console.log(undefined == 0); // false (3)

/*
Comparisons (1) and (2) return false because undefined gets converted to NaN and NaN is a special numeric value which returns false for all comparisons.

The equality check (3) returns false because undefined only equals null, undefined, and no other value.

*/