Basic JavaScript Programs

 

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()
// the hello world program
console.log('Hello World');
2. Using alert()
// the hello world program
alert("Hello, World!");
3. Using document.write()
// the hello world program
document.write('Hello, World!');


2)Program to Add Two Numbers

1.Add Two Numbers
const num1 = 5;
const num2 = 3;

// add two numbers
const sum = num1 + num2;

// display the sum
console.log('The sum of ' + num1 + ' and ' + num2 + ' is: ' + sum);
2.Add Two Numbers Entered by the User
// store input numbers
const num1 = parseInt(prompt('Enter the first number '));
const num2 = parseInt(prompt('Enter the second number '));

//add two numbers
const sum = num1 + num2;

// display the sum
console.log('The sum of ${num1} and ${num2} is ${sum}');


3)Program to Swap Two Variables

1.Using a Temporary Variable
//JavaScript program to swap two variables

//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');

//create a temporary variable
let temp;

//swap variables
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

//JavaScript program to swap two variables

//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');

//using destructuring assignment
[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
// taking kilometers input from the user
const kilometers = prompt("Enter value in kilometers: ")

// conversion factor
const factor = 0.621371

// calculate miles
const miles = kilometers * factor

console.log(`${kilometers} kilometers is equal to ${miles} miles.`);
Miles to Kilometers
// taking miles input from the user
const miles = prompt("Enter value in miles: ")

// conversion factor
const factor = 0.621371

// calculate kilometers
const kilometers = miles / factor

console.log('${miles} miles is equal to ${kilometers} kilometers.');


5)Program to Convert Celsius to Fahrenheit

Celsius to Fahrenheit
// program to convert celsius to fahrenheit
// ask the celsius value to the user 
const celsius = prompt("Enter a celsius value: ");

// calculate fahrenheit
const fahrenheit = (celsius * 1.8) + 32

// display the result
console.log('${celsius} degree celsius is equal to ${fahrenheit} degree fahrenheit.');
Fahrenheit to Celsius
// program to convert fahrenheit to celsius
// ask the fahrenheit value to the user 
const fahrenheit = prompt("Enter a fahrenheit value: ");

// calculate celsius
const celsius = (fahrenheit - 32) / 1.8

// display the result
console.log('${fahrenheit} degree fahrenheit is equal to ${celsius} degree celsius.');



Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

AJ Facebook
Checkout Our Facebook Page
AJ Blogs
Checkout Our Instagram Page