JavaScript Revision (Part-1): Key Concepts and Learnings
Sameer Dixit
Software Engineer at Simplotel | JP Morgan & Chase Co. Code for Good Runner Up | I don’t just think outside the box—I design it, build it, and upgrade it to version 2.0!
Today, I dedicated time to revisiting some fundamental JavaScript concepts. Here's a brief overview of the key points I covered:
1. Variables and Data Types
- var, let, const: Different ways to declare variables with distinct scopes and mutability.
- Data Types: Strings, Numbers, Booleans, Objects, and Undefined.
2. Functions
- Function Declaration:
function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(5)); // Output: 120
- Function Expression:
const gcd = function(a, b) {
if (!b) return a;
return gcd(b, a % b);
};
console.log(gcd(48, 18)); // Output: 6
- Arrow Function:
const fibonacci = (n, memo = {}) => {
if (n in memo) return memo[n];
if (n <= 2) return 1;
memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo);
return memo[n];
};
console.log(fibonacci(10)); // Output: 55
3. Arrays
- Usage: Storing multiple values.
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // Output: Apple
- Methods: push(), pop(), shift(), unshift().
- Complex Example: Flattening a nested array
```javascript
const flattenArray = (arr) => {
return arr.reduce((flat, toFlatten) => {
return flat.concat(Array.isArray(toFlatten) ? flattenArray(toFlatten) : toFlatten);
}, []);
};
console.log(flattenArray([1, [2, [3, 4], 5]])); // Output: [1, 2, 3, 4, 5]
4. Objects
- Structure: Key-value pairs.
let person = {
name: "John",
age: 30,
country: "USA"
};
console.log(person.name); // Output: John
Example: Merging two objects
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
const merged = { ...obj1, ...obj2 };
console.log(merged); // Output: { a: 1, b: 3, c: 4 }
5. Hoisting
- Concept: Variables and function declarations are moved to the top of their containing scope during the compile phase.
领英推荐
console.log(sum(5, 10)); // Output: 15
function sum(a, b) {
return a + b;
}
console.log(x); // Output: undefined
var x = 5;
6. Data Type Conversion
Data type conversion is a critical aspect of JavaScript that allows you to convert one data type to another. There are two types of data type conversions in JavaScript: implicit and explicit.
Implicit Conversion
Also known as type coercion, implicit conversion occurs when JavaScript automatically converts data types for you.
- String Conversion: If you use the + operator with a string and a number, JavaScript converts the number to a string.
let result = "5" + 5; // "55"
let result = "Hello" + 5; // "Hello5"
- Number Conversion: If you use the -, *, / operators, JavaScript converts strings to numbers.
let result = "5" - 5; // 0
let result = "10" * "2"; // 20
Explicit Conversion
Explicit conversion is when you manually convert data from one type to another using built-in JavaScript functions.
- String to Number: Using Number(), parseInt(), and parseFloat().
let num = Number("123"); // 123
let num = parseInt("123.45"); // 123
let num = parseFloat("123.45"); // 123.45
- Number to String: Using String() or .toString().
let str = String(123); // "123"
let str = (123).toString(); // "123"
- Boolean Conversion: Using Boolean()
let bool = Boolean(1); // true
let bool = Boolean(0); // false
let bool = Boolean("Hello"); // true
let bool = Boolean(""); // false
- Automatic Conversion in Conditional Statements: Values like 0, "", null, undefined, and NaN are converted to false in conditional statements.
if (0) {
console.log("This won't be logged");
}
if ("") {
console.log("This won't be logged either");
}