js notes
js notes
Block Scope
Before ES6 (2015), JavaScript did not have Block Scope.
ES6 introduced the two new JavaScript keywords: let and const.
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.
let x = 0;
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
What is Good?
let and const have block scope.
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
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
console.log(i);
In this example:
i <= 10; is the condition. The loop will continue to execute as long as i is less than or equal to
10.
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.
Expression 1 is executed (one time) before the execution of the code block.
Expression 3 is executed (every time) after the code block has been executed.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
let text = "";
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
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.
The switch statement is used to perform different actions based on different conditions.
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:
// 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.