Content

  • JavaScript Introduction
  • Simple JavaScript Examples
  • Data Types, Variables in JavaScript
  • Arrays and Objects in JavaScript
  • if..else statement in JavaScript
  • Loops in JavaScript

JavaScript Introduction

  • JavaScript is mainly used to program the behavior of web pages
  • Using JavaScript, we can change and update HTML and CSS.
  • Javascript frameworks (e.g. - node.js) also can be used to develop dynamic web applications.
  • JavaScript can be use to implement API, web services etc.

Simple JavaScript Examples

Output Code

JavaScript can change my content.

<p id="demo"></p>
<button type="button" onclick='document.getElementById("demo").innerHTML = "Hello JavaScript!"'>Click Me!</button>

<button type="button" onclick="document.getElementById('demo1').innerHTML = Date()"> Click me to display Date and Time.</button>
<p id="demo1"></p>

Data Types, Variables in JavaScript

JavaScript has dynamic types. This means that the same variable can be used to hold different data types
  • let - We can change its values, but it cannot be redeclared in the same scope.
  • const - It is a variable type assigned to data whose value cannot and will not be changed throughout the program
  • A variable name cannot start with a number.

Arrays and Objects in JavaScript

These are reference data types in JS
Arrays are the objects in which we can store multiple values in a single variable
//There are two ways to declare Array
let cities = new Array();
let cities = [];
cities =['Kolhapur', 'Pune', 'Mumbai']
//How to access array elements
console.log(cities[0])
console.log(cities[1])
//check log in browser for output
Object holds multiple values, arrays, functions etc.
//There are two ways to declare Object
let car = new Object();
let car = {};
car = { 
    name: "A7",
    brand: "Audi",
    capacity: 5
  };
//How to access properties of an Object
console.log(car['name'])
console.log(car['brand'])
//check log in browser for output

if..else statement in JavaScript

if..else and if..else if..else are conditional statements in JavaScript
Conditional statements are used to decide the execution flow, based on different conditions
Example : if..else statement
let age = 25;
if( age > 18)
{
console.log("My age is greater than 18");
}
else
{
console.log("My age is less than or 18");
}
//check log in browser for output
We can use "else if" condition when we want to apply second level conditions after the if statement
Example : if..else if..else statement
let age = 25;
if( age > 18)
{
console.log("My age is greater than 18");
}
else if(age >21)
{
console.log("My age is greater than 21");
}
else
{
console.log("My age is less than 18");
}
//check log in browser for output

Loops in JavaScript

Loops are used when we want to repeat an action or run some code multiple times
  • for Loop
  • for…in Loop
  • foreach Loop
  • while Loop
  • do…while Loop
for Loop
The for loop is used when we want to execute the set of statements for a certain number of times.
Example :
for(var j=1; j<=7; j++) 
{
console.log(j);
}
//check log in browser for output

for…in Loop
It is a special type of loop, used when we want to iterates over the properties of an object or the elements of an array.
Example :
var person = {name: "Shree", language: "JavaScript", age: 28};
for(var p in person) 
{  
console.log( p + " = " + person[p]); 
}
//check log in browser for output

foreach Loop
With the help of forEach loop, we can execute a function on each item within an array.
Example :
const  evenno = [2,4,6,8,10];
evenno.forEach(showen);
 
function showen(item, index)
{
console.log(item);
}
//check log in browser for output

while Loop
A while loop is used when we do not know how many times a certain block of code should execute.
Example :
let x = 1, y = 6;
while (x <= y) 
{
console.log(x);
x += 1;
}
//check log in browser for output

do..While Loop
In this loop, the body inside the do statement is executed first,then the condition is evaluated.
Example :
let a = 1;
let b =3;
do {
console.log(a);
a++;
} while(a <= b);
//check log in browser for output