Introduction to JavaScript
In this session you will learn the following concepts in JavaScript
Declaring variables in JavaScript (var, let and const)
var - properties
- Function-scoped: Variables declared with var are accessible within the entire function where they're declared, even before their line of code is reached (hoisting).
- Reassignable: Can have their values changed multiple times.
- Error-prone: Hoisting can lead to unexpected behavior if not used carefully.
- Best avoided in modern JavaScript: let and const offer better scoping and prevent common pitfalls.
let - properties
- Block-scoped: Variables declared with let are only accessible within the block of code where they're declared (e.g., within curly braces {}).
- Reassignable: Can be reassigned new values after initial declaration.
- Preferred for most variables: Offers better control over variable scope and prevents accidental overwrites.
const properties
- Block-scoped: Like let, but with an added layer of protection.
- Reassignable: Can be reassigned new values after initial declaration.
- Preferred for most variables: Offers better control over variable scope and prevents accidental overwrites.
Best Practices:
- Prefer const by default: Start with const for variables that don't need reassignment.
- Use let for variables that need to be reassigned: Employ let when values need to change.
- Avoid var in modern JavaScript: Due to its potential for scoping issues.
const name="Satish";
let marks=20;
var number = 21;
if else and nested if else statement in JavaScript
Basic if Statement Syntax
if (condition) {
// Code to execute if the condition is true
}
if else Statement Syntax
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
if...else if...else Statement Syntax
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition1 is false and condition2 is true
} else {
// Code to execute if both condition1 and condition2 are false
}
Exercise 1 - if else statment
let x=5
if(x==2)
{
console.log('x equals 5');
}
else
{
console.log('x is a wrong value')
}
Output
x is a wrong valueExercise 2 - if...else if...else statment
let x=5
if(x==2)
{
console.log('x is 2');
}else if(x==1)
{
console.log('x is 1')
}
else
{
console.log('wrong value');
}
Output
wrong valueSwitch statement in JavaScript
Switch case Statement Syntax
switch (expression) {
case value1:
// Code to execute if expression matches value1
break;
case value2:
// Code to execute if expression matches value2
break;
// ... more cases
default:
// Code to execute if expression doesn't match any of the cases
}
Exercise 1 - Switch Case with Integer
let x=2
switch(x)
{
case 1:
console.log('x is 1');
break;
case 2:
console.log('x is 2');
break;
default:
console.log('x is not good value');
break;
}
Output
x is 2Exercise 2 - Switch Case with Character Input
let x='c'
switch(x)
{
case 'a':
console.log('x is a');
break;
case 'c':
console.log('x is c');
break;
default:
console.log('x is not good value');
break;
}
Output
x is cSimple for loop in JavaScript
Syntax for simple for loop
for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}
Exercise 1 - Use simple for loop and print the numbers from 0 to 4
let i=0
for(i=0;i<5;i++)
{
console.log(i);
}
Output
01
2
3
4
While loop in JavaScript
Syntax for while loop
while (condition) {
// Code to be executed repeatedly
}
Exercise 1 - Use simple while loop and print the numbers from 0 to 4
let x=0;
while (x<5)
{
console.log(x);
x++
}
Output
01
2
3
4
do While loop in JavaScript
Syntax for do while loop
do {
// Code to be executed at least once
} while (condition);
Exercise 1 - Use simple do while loop and print the numbers from 0 to 4
let x=0;
do
{
console.log(x);
x++
}while(x<5)
Output
01
2
3
4
Declaring Arrays in JavaScript
Syntax for Array Declaration
// 1. Using square brackets:
let myArray = []; // Empty array
let numbers = [1, 2, 3, 4, 5]; // Array with initial values
// 2. Using the Array constructor:
let colors = new Array("red", "green", "blue");
Exercise 1 - Printing the elements inside an array using a simple for loop
const k = [1,2,3,4,5,6]
for(let i=0;i<k.length;i++)
{
console.log(k[i]);
}
Output
12
3
4
5
6
Exercise 2 - Printing elements inside an array using for in loop
const k = [1,2,3,4,5,6]
for(var j in k)
{
console.log(k[j]);
}
Output
12
3
4
5
6
Exercise 3 - Printing elements inside an array using for of loop
const k = [1,2,3,4,5,6]
for(var j of k)
{
console.log(j);
}
Output
12
3
4
5
6