0% found this document useful (0 votes)
42 views

In JavaScript

In JavaScript, 'var', 'let', and 'const' are used for variable declarations, differing in scope, hoisting, and mutability. 'var' is function-scoped and allows redeclaration, 'let' is block-scoped and does not allow redeclaration, while 'const' is also block-scoped and cannot be reassigned after its initial assignment. The 'typeof' operator checks the data type of a variable or value, returning a string representation, but has quirks such as treating arrays and null as objects.

Uploaded by

vihave1614
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)
42 views

In JavaScript

In JavaScript, 'var', 'let', and 'const' are used for variable declarations, differing in scope, hoisting, and mutability. 'var' is function-scoped and allows redeclaration, 'let' is block-scoped and does not allow redeclaration, while 'const' is also block-scoped and cannot be reassigned after its initial assignment. The 'typeof' operator checks the data type of a variable or value, returning a string representation, but has quirks such as treating arrays and null as objects.

Uploaded by

vihave1614
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/ 5

1.let vs var vs const ???

In JavaScript, let, var, and const are used to declare variables,


but they have some differences in terms of scope, hoisting, and
mutability.

1. var:
 Variables declared with var are function-scoped,
meaning they are only visible within the function where
they are declared. If declared outside any function, the
variable is global.
 var is hoisted to the top of its scope, which means you
can use the variable before it is declared in the code.
 var allows redeclaration of the same variable within the
same scope.
Ex 1:
function example() {
if (true) {
var x = 10;
}
console.log(x); // Outputs 10, despite being declared inside the 'if' block
}

Ex 2:
function example() {
console.log(x); // Outputs 'undefined' due to hoisting
var x = 10;
console.log(x); // Outputs 10
}
example();
2 let:
 Variables declared with let have block scope, meaning they
are only visible within the block (enclosed by curly braces)
where they are defined.
 let is also hoisted, but it is not initialized until the actual
declaration is encountered in the code. This is known as the
"temporal dead zone."
 Unlike var, let does not allow redeclaration of the same
variable within the same scope.

Ex 1:
function example() {
if (true) {
let x = 10;
}
console.log(x); // Error: x is not defined
}

Ex 2:
function example() {
console.log(x); // ReferenceError: Cannot access 'x' before initialization
let x = 10;
console.log(x); // Outputs 10
}
example();
3.const:
Variables declared with const are also block-scoped.
 const is used for constants, and once a value is
assigned to a const variable, it cannot be reassigned.
 Like let, const is hoisted and subject to the temporal
dead zone.
 It is important to note that while the variable itself is
immutable, the value it holds may not be if it is an
object or an array. This means you cannot reassign a
new value to a const variable, but you can modify its
properties or elements.
Ex 1:
const PI = 3.14;
PI = 4; // Error: Assignment to a constant variable
const obj = { key: 'value' };
obj.key = 'new value'; // Valid, because the object itself is mutable

2.typeOf ()??

In JavaScript, the `typeof` operator is used to determine the data type


of a given operand or expression. It is often used to check the type of a
variable or value at runtime. The syntax for `typeof` is as follows:
The result of the `typeof` operation is a string representing the data
type.
1. **Checking Primitive Types:**
typeof 42; // "number"
typeof "hello"; // "string"
typeof true; // "boolean"
```

2. **Checking Objects:**

typeof {}; // "object"


typeof []; // "object" (Arrays are objects in JavaScript)
typeof null; // "object" (Note: This is a known quirk in JavaScript)

3. **Checking Functions:**

typeof function(){}; // "function"

4. **Checking Undefined and Variables:**

let x;
typeof x; // "undefined"

5. **Checking for `null`:**

`javascript let y = null;


typeof y; // "object" (Another quirk, `null` is considered an
object)
```

It's important to note that `typeof` returns a string representation of


the data type, and it doesn't distinguish between different types of
objects (e.g., arrays, objects, and functions all have a type of "object").
Also, `typeof` is not always a reliable way to determine the exact nature
of an object in JavaScript.
For more precise type checking, consider using other methods or
libraries, such as the `instanceof` operator or external libraries like
`lodash` or `prop-types` (in React applications).).

You might also like