Table of Content
- Program To Print Hello World
- Program to Add Two Numbers
- Program to Swap Two Variables
- Program to Convert Kilometers to Miles
- Program to Convert Celsius to Fahrenheit
1) Program To Print Hello World
1.Using console.log() |
console.log('Hello World');
|
2. Using alert() |
alert("Hello, World!");
|
3. Using document.write() |
document.write('Hello, World!');
|
2)Program to Add Two Numbers
1.Add Two Numbers |
const num1 = 5;
const num2 = 3;
const sum = num1 + num2;
console.log('The sum of ' + num1 + ' and ' + num2 + ' is: ' + sum);
|
2.Add Two Numbers Entered by the User |
const num1 = parseInt(prompt('Enter the first number '));
const num2 = parseInt(prompt('Enter the second number '));
const sum = num1 + num2;
console.log('The sum of ${num1} and ${num2} is ${sum}');
|
3)Program to Swap Two Variables
1.Using a Temporary Variable |
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
let temp;
temp = a;
a = b;
b = temp;
console.log('The value of a after swapping: ${a}');
console.log('The value of b after swapping: ${b}');
|
2.Using es6(ES2015) Destructuring assignment |
|
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
[a, b] = [b, a];
console.log('The value of a after swapping: ${a}');
console.log('The value of b after swapping: ${b}');
4)Program to Convert Kilometers to Miles
Kilometers to Miles |
const kilometers = prompt("Enter value in kilometers: ")
const factor = 0.621371
const miles = kilometers * factor
console.log(`${kilometers} kilometers is equal to ${miles} miles.`);
|
Miles to Kilometers |
const miles = prompt("Enter value in miles: ")
const factor = 0.621371
const kilometers = miles / factor
console.log('${miles} miles is equal to ${kilometers} kilometers.');
|
5)Program to Convert Celsius to Fahrenheit
Celsius to Fahrenheit |
const celsius = prompt("Enter a celsius value: ");
const fahrenheit = (celsius * 1.8) + 32
console.log('${celsius} degree celsius is equal to ${fahrenheit} degree fahrenheit.');
|
Fahrenheit to Celsius |
const fahrenheit = prompt("Enter a fahrenheit value: ");
const celsius = (fahrenheit - 32) / 1.8
console.log('${fahrenheit} degree fahrenheit is equal to ${celsius} degree celsius.');
|