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

js notes

The document explains the use of the 'let' keyword in JavaScript, introduced in ES6, which provides block scope and cannot be redeclared in the same scope. It contrasts 'let' with 'var', highlighting that 'var' has global scope and can be redeclared, leading to potential issues. Additionally, the document covers various loop structures, conditional statements, and the concept of objects in JavaScript, illustrating how objects are used to model real-world entities.

Uploaded by

btsforevervii
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)
5 views

js notes

The document explains the use of the 'let' keyword in JavaScript, introduced in ES6, which provides block scope and cannot be redeclared in the same scope. It contrasts 'let' with 'var', highlighting that 'var' has global scope and can be redeclared, leading to potential issues. Additionally, the document covers various loop structures, conditional statements, and the concept of objects in JavaScript, illustrating how objects are used to model real-world entities.

Uploaded by

btsforevervii
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/ 13

JavaScript Let

The let keyword was introduced in ES6 (2015)

Variables declared with let have Block Scope

Variables declared with let must be Declared before use

Variables declared with let cannot be Redeclared in the same scope

Block Scope
Before ES6 (2015), JavaScript did not have Block Scope.

JavaScript had Global Scope and Function Scope.

ES6 introduced the two new JavaScript keywords: let and const.

These two keywords provided Block Scope in JavaScript:

Example

Variables declared inside a { } block cannot be accessed from outside the block:

{
let x = 2;
}
// x can NOT be used here

Global Scope
Variables declared with the var always have Global Scope.

Variables declared with the var keyword can NOT have block scope:

Example

Variables declared with var inside a { } block can be accessed from outside the block:

{
var x = 2;
}
// x CAN be used here
Cannot be Redeclared
Variables defined with let can not be redeclared.

You can not accidentally redeclare a variable declared with let.

With let you can not do this:

let x = "John Doe";

let x = 0;

Variables defined with var can be redeclared.

With var you can do this:

var x = "John Doe";

var x = 0;

Redeclaring Variables
Redeclaring a variable using the var keyword can impose problems.

Redeclaring a variable inside a block will also redeclare the variable outside the block:

Example

var x = 10;
// Here x is 10

{
var x = 2;
// Here x is 2
}

// Here x is 2

Redeclaring a variable using the let keyword can solve this problem.

Redeclaring a variable inside a block will not redeclare the variable outside the block:
Example

let x = 10;
// Here x is 10

{
let x = 2;
// Here x is 2
}

// Here x is 10

Difference Between var, let and const

Scope Redeclare Reassign


var No Yes Yes
let Yes No Yes
const Yes No No

What is Good?
let and const have block scope.

let and const can not be redeclared.

let and const must be declared before use.

What is Not Good?


var does not have to be declared.

Browser Support
The let and const keywords are not supported in Internet Explorer 11 or earlier.

The following table defines the first browser versions with full support:
Program to check whether give string is empty or not
function isBlank(str) {
return str.trim() === '';
}

// Examples
console.log(isBlank("")); // true
console.log(isBlank(" ")); // true
console.log(isBlank("Hello")); // false
console.log(isBlank(" World ")); // false

This function, isBlank, takes a string as input (str). It uses the trim()
method to remove whitespace from both ends of the string. Then, it
checks if the resulting string is equal to an empty string (''). If it is, the
original string was either empty or contained only whitespace, and the
function returns true. Otherwise, it returns false.
For loop

for (initialization; condition; increment/decrement) {

// code to be executed in each iteration

Let's break down each part:

initialization: This statement is executed only once before the loop starts. It's typically used to
declare and initialize a loop counter variable.

condition: This expression is evaluated before each iteration of the loop. If the condition is
true, the code inside the loop's curly braces {} is executed. 1 If the condition is false, the loop
terminates.
increment/decrement: This statement is executed at the end of each iteration. It's usually used
to update the loop counter variable (e.g., incrementing or decrementing it).

Now, here's a for loop that prints the numbers 1 to 10 in the console:

JavaScript

for (let i = 1; i <= 10; i++) {

console.log(i);

In this example:

let i = 1; initializes a variable i to 1. This is our loop counter.

i <= 10; is the condition. The loop will continue to execute as long as i is less than or equal to
10.

i++; increments the value of i by 1 after each iteration.

console.log(i); is the code that gets executed in each iteration, which prints the current value of
i to the console.

When you run this code, you'll see the numbers 1 through 10 printed in your browser's console or
your Node.js terminal.

Different Kinds of Loops


JavaScript supports different kinds of loops:

 for - loops through a block of code a number of times


 for/in - loops through the properties of an object
 for/of - loops through the values of an iterable object
 while - loops through a block of code while a specified condition is true
 do/while - also loops through a block of code while a specified condition is true

The For Loop

The for statement creates a loop with 3 optional expressions:

for (expression 1; expression 2; expression 3) {


// code block to be executed
}

Expression 1 is executed (one time) before the execution of the code block.

Expression 2 defines the condition for executing the code block.

Expression 3 is executed (every time) after the code block has been executed.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For Loop</h2>

<p id="demo"></p>

<script>
let text = "";

for (let i = 0; i < 5; i++) {


text += "The number is " + i + "<br>";
}

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>
JavaScript For Loop
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4

JavaScript While Loop

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9

Conditional statements are used to perform different actions based on different conditions.

Conditional Statements
Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In JavaScript we have the following conditional statements:
 Use if to specify a block of code to be executed, if a specified condition is true
 Use else to specify a block of code to be executed, if the same condition is false
 Use else if to specify a new condition to test, if the first condition is false
 Use switch to specify many alternative blocks of code to be executed
The switch statement is described in the next chapter.

The if Statement
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate a JavaScript error.

JavaScript Switch Statement

The switch statement is used to perform different actions based on different conditions.

The JavaScript Switch Statement


Use the switch statement to select one of many code blocks to be executed.
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
This is how it works:
 The switch expression is evaluated once.
 The value of the expression is compared with the values of each case.
 If there is a match, the associated block of code is executed.
 If there is no match, the default code block is executed.
Example
The getDay() method returns the weekday as a number between 0 and 6.
(Sunday=0, Monday=1, Tuesday=2 ..)
This example uses the weekday number to calculate the weekday name:

JavaScript switch
Today is Thursday

Objects in JavaScript
In JavaScript, an object is a collection of properties, where each property is defined as a key-
value pair. Objects are a fundamental part of JavaScript and are used to store and manage data in
a structured way. Here are some key points about objects in JavaScript:
1. Definition
 An object is a standalone entity, with properties and type. It is similar to real-life objects,
which have characteristics (properties) and behaviors (methods).
Creating Objects
There are several ways to create objects in JavaScript:
Using Object Literal Syntax:

Using the new Object() Syntax:

Using a Constructor Function:


why javascript is called object based programming language ..show by example.

JavaScript is often referred to as an "object-based programming language" because it uses


objects as a fundamental part of its structure. In JavaScript, almost everything is an object, and
you can create and manipulate objects to model real-world entities and behaviors.
Example: Modeling a Car Object
Let's illustrate this concept by creating a simple Car object that represents a car with properties
and methods.

Car object using an object literal


const car = {
make: "Toyota",
model: "Camry",
year: 2020,
color: "blue",
mileage: 15000,

// Method to display car details


displayDetails: function() {
console.log(`Car Details: ${this.year} ${this.make} ${this.model} ($
{this.color}) - Mileage: ${this.mileage} miles`);
},

// Method to drive the car, which updates the mileage


drive: function(miles) {
this.mileage += miles;
console.log(`You drove the car for ${miles} miles.`);
}
};

// Accessing properties
console.log(car.make); // Output: Toyota
console.log(car['model']); // Output: Camry
// Calling methods
car.displayDetails(); // Output: Car Details: 2020 Toyota Camry (blue) -
Mileage: 15000 miles
car.drive(100); // Output: You drove the car for 100 miles.
car.displayDetails(); // Output: Car Details: 2020 Toyota Camry (blue) -
Mileage: 15100 miles

Explanation:
1. Object Creation:
o We create a car object using an object literal. This object has properties like
make, model, year, color, and mileage.
2. Methods:
o The car object has two methods:
 displayDetails(): This method logs the details of the car to the console.
 drive(miles): This method simulates driving the car by updating the
mileage property.
3. Accessing Properties and Methods:
o We can access the properties of the car object using dot notation (e.g., car.make)
or bracket notation (e.g., car['model']).
o We can call the methods of the car object using dot notation (e.g.,
car.displayDetails()).
Conclusion:
This example illustrates how JavaScript allows you to create and manipulate objects,
encapsulating both data (properties) and behavior (methods) in a single entity. This object-
based approach makes it easier to model real-world scenarios and manage complex data
structures, which is a key feature of JavaScript as an object-based programming language.

You might also like