0% found this document useful (0 votes)
2 views6 pages

Javascript Important Topic

The document discusses the advantages and disadvantages of using `const` and `let` in JavaScript, highlighting that `const` provides immutability and safety, while `let` offers flexibility and block scope. It also explains the concept of the Temporal Dead Zone (TDZ) which affects `let` and `const` variables, preventing access before initialization. Additionally, it covers the differences between the `split()`, `slice()`, and `splice()` methods, as well as the `Math` object and truthy/falsy values in JavaScript.

Uploaded by

abhishek.skd13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Javascript Important Topic

The document discusses the advantages and disadvantages of using `const` and `let` in JavaScript, highlighting that `const` provides immutability and safety, while `let` offers flexibility and block scope. It also explains the concept of the Temporal Dead Zone (TDZ) which affects `let` and `const` variables, preventing access before initialization. Additionally, it covers the differences between the `split()`, `slice()`, and `splice()` methods, as well as the `Math` object and truthy/falsy values in JavaScript.

Uploaded by

abhishek.skd13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Javascript topic’s

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.

### Why Not Just Use `let`?

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.

How TDZ Works (Temporal dead zone)


In JavaScript, the Temporal Dead Zone (TDZ) refers to the period of time during which a variable is in a
scope but cannot be accessed because it has not yet been initialized. This period starts from the
beginning of the block in which the variable is declared (or when the block is entered) and ends when
the declaration is actually executed.
Variables declared with let and const are subject to the TDZ, unlike those declared with var.

Here's a simple example to illustrate the TDZ:

javascript

console.log(a); // ReferenceError: Cannot access 'a' before initialization

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.

 Attempting to access a before its declaration results in a ReferenceError.

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

console.log(x); // ReferenceError: Cannot access 'x' before initialization

let x = 10;

function example() {

console.log(y); // ReferenceError: Cannot access 'y' before initialization

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).

What is the difference between


splice, slice, and split method in Javascript ?
today, I want to talk about split(), splice(), and slice() method. You know there are lots of functions with scarily similar
names in land of javascript. That's why javascript methods can be confused each other sometimes.

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.

 First argument that represent where to start selection.

 Second argument that represent where to end selection.

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.

 First argument is index and requried.

 Second argument is optional and represent the number of items be removed.

 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) , (string method) in


javascript!
Template literals and string methods in JavaScript provide powerful ways to work with strings. Here are brief
explanations and examples for each:

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

const name = "John";

const age = 30;

const message = `My name is ${name} and I am ${age} years old.`;

console.log(message); // Output: My name is John and I am 30 years old.

Mathematical operator in javascript!


 addition operator +
 Subtraction operator -
 multiplication operator *
 division operator /
 remainder operator %
 exponentiation operator ** 5 ** 2 = 25 added in 2016 ecma update .

Math Object in javascript!


The `Math` object in JavaScript is a built-in object that has properties and methods for mathematical constants
and functions.

 properties
 method

Properties

- `Math.E`: Euler's number, approximately 2.718.


- `Math.LN2`: Natural logarithm of 2, approximately 0.693.
- `Math.LN10`: Natural logarithm of 10, approximately 2.302.
- `Math.LOG2E`: Base 2 logarithm of E, approximately 1.442.
- `Math.LOG10E`: Base 10 logarithm of E, approximately 0.434.
- `Math.PI`: Ratio of the circumference of a circle to its diameter, approximately 3.14159.
- `Math.SQRT1_2`: Square root of 1/2, approximately 0.707.
- `Math.SQRT2`: Square root of 2, approximately 1.414.

Methods

- `Math.abs(x)`: Returns the absolute value of a number.


- `Math.acos(x)`: Returns the arccosine of a number.
- `Math.asin(x)`: Returns the arcsine of a number.
- `Math.atan(x)`: Returns the arctangent of a number.
- `Math.atan2(y, x)`: Returns the arctangent of the quotient of its arguments.
- `Math.ceil(x)`: Returns the smallest integer greater than or equal to a number.
- `Math.cos(x)`: Returns the cosine of a number.
- `Math.exp(x)`: Returns E raised to the power of a number.
- `Math.floor(x)`: Remove any integers after decimals like 2.99 = 2 .
- `Math.log(x)`: Returns the natural logarithm (base E) of a number.
- `Math.max(x, y, ...values)`: Returns the largest of zero or more numbers.
- `Math.min(x, y, ...values)`: Returns the smallest of zero or more numbers.
- `Math.pow(x, y)`: Returns base to the exponent power.
- `Math.random()`: Returns a pseudo-random number between 0 and 1.
- `Math.round(x)`: Returns the value of a number rounded to the nearest integer.
- `Math.sin(x)`: Returns the sine of a number.
- `Math.sqrt(x)`: Returns the square root of a number.

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

let angle = Math.PI / 2; // 90 degrees in radians


console.log(Math.sin(angle)); // 1

console.log(Math.pow(2, 3)); // 8
console.log(Math.sqrt(16)); // 4

console.log(Math.random()); // Random number between 0 and 1


```

The `Math` object provides a wide range of functionalities for performing mathematical operations and is
a fundamental part of JavaScript's standard library.

Truthy and falsy value in javascript!


Falsy values are:

 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.

You might also like