Javascript Important Topic
Javascript Important Topic
Certainly! Let's break down the advantages and disadvantages of using `const`
and `let` in JavaScript in a simple and understandable way.
`const` (Constant)
Advantages:
1. **Immutability:** Once a `const` variable is assigned a value, it cannot be reassigned. This makes the code more
predictable and easier to debug, as the value cannot change unexpectedly.
2. **Readability:** Using `const` signals to other developers that the value should not change, making the code
easier to understand.
3. **Safety:** Helps prevent accidental reassignments and bugs that can occur from changing the value of a variable.
Disadvantages:
1. **Rigidity:** You cannot change the value once it is set, which might be limiting in situations where you need a
variable to be mutable.
`let` (Let)
Advantages:
1. **Flexibility:** You can reassign values to a `let` variable, making it suitable for situations where the value needs
to change over time (e.g., counters, iterators).
2. **Block Scope:** `let` is block-scoped, meaning it only exists within the block it is defined in. This reduces the risk
of variable conflicts and makes the code cleaner.
Disadvantages:
1. **Potential for Bugs:** Since `let` allows reassignment, it can lead to bugs if the variable is unintentionally
changed.
2. **Less Predictability:** It might be harder to track changes to a `let` variable, especially in larger codebases,
making debugging more challenging.
While `let` is more flexible, using `const` where appropriate can improve code quality. Here’s why:
1. **Intentionality:** Using `const` shows the intention that the value should not change, making the code more
understandable and maintainable.
2. **Error Prevention:** `const` helps catch errors early by preventing accidental reassignment of variables that
should remain constant.
3. **Best Practices:** Modern JavaScript best practices suggest using `const` by default and only using `let` when you
know the value needs to change. This approach leads to cleaner, more robust code.
Conclusion
Both `const` and `let` have their uses. `const` is great for values that should not change, adding stability and clarity to
the code. `let` is useful for variables that need to change. Using them appropriately helps create better, more
maintainable JavaScript code.
The term "Temporal Dead Zone" (TDZ) in JavaScript refers to the period of time during which variables declared with
`let` and `const` cannot be accessed before they are initialized. This concept is crucial to understanding how variable
declarations work in modern JavaScript.
javascript
let a = 3;
In this example:
The variable a is in the TDZ from the start of the block until the line let a = 3 is executed.
1. **Declaration**: When the JavaScript engine parses the code, it recognizes variables declared with `let` and
`const` and sets up the memory space for them. However, unlike `var`, these variables are not initialized with a
default value of `undefined`.
2. **Initialization**: The actual value is assigned to the variable when the execution reaches the declaration
statement.
3. **Accessing Before Initialization**: Any attempt to access these variables before they are initialized results in a
`ReferenceError`.
Example
```java script
let x = 10;
function example() {
const y = 20;
example();
```
In this example:- Accessing `x` before its `let` declaration results in a `ReferenceError`.
- Similarly, accessing `y` before its `const` declaration inside the function results in a `ReferenceError`.
Key Points
- **TDZ for `let` and `const`**: Only variables declared with `let` and `const` have a TDZ. Variables declared with `var`
are hoisted and initialized with `undefined`, so they don't have a TDZ.
- **Function Scope and Block Scope**: The TDZ applies in both function scope and block scope. Variables declared
inside a block (e.g., within `{}`) also have a TDZ if declared with `let` or `const`.
- **Best Practices**: To avoid issues with TDZ, always declare variables at the beginning of their scope. This practice
enhances code readability and reduces the chance of encountering `ReferenceError`.
Understanding the Temporal Dead Zone is important for writing robust JavaScript code, especially as the use of
`let` and `const` has become more prevalent with the introduction of ES6 (ECMAScript 2015).
Split()
Split is a function that split the given string into an array of substrings. The split method doesn't change the original
string array and will return the new array.
Slice()
Slice method doesn't change the original array. It is method of both arrays and strings. Slice method can take two
arguments. First argument is required, second argument is optional.
Splice()
Splice method changes the original array. It can remove element, replace existing elements, or add new elements to
array. It can take 3+ arguments.
Third argument is optional and represent the number of items be added. Argument can be increased.
Sometimes we want to remove items from array and we can use pop() and shift() methods instead
of splice() method. But it is easy to use and we can remove more than one item or add to the array.
Conclusion
Split() method is used for strings array, slice() method is used for both arrays and strings. Splice() method is used
for only arrays.
Template Literals
Template literals are enclosed by backticks (`) and allow embedded expressions, multi-line strings, and string
interpolation using the ${} syntax.
Example:
javascript
Copy code
properties
method
Properties
Methods
Example Usage
javascript
let num = -5.5;
console.log(Math.abs(num)); // 5.5
console.log(Math.ceil(num)); // -5
console.log(Math.floor(num)); // -6
console.log(Math.round(num)); // -6
console.log(Math.pow(2, 3)); // 8
console.log(Math.sqrt(16)); // 4
The `Math` object provides a wide range of functionalities for performing mathematical operations and is
a fundamental part of JavaScript's standard library.
0
-0
NaN (no a number)
‘ ‘ (empty string not with space cuz space is a string)
Undefined
Null
These are falsy value in javascript ! except these all values are true even negative ones.