Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

JavaScript Program To Print Hello World

This document provides JavaScript programs for various tasks including printing 'Hello, World!', adding two numbers, calculating the area of a triangle, converting kilometers to miles, and converting Celsius to Fahrenheit. Each section includes example code and explanations of the concepts involved, such as variables, operators, and mathematical functions. The document serves as a practical guide for beginners to understand basic JavaScript programming techniques.

Uploaded by

datalogdigital
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JavaScript Program To Print Hello World

This document provides JavaScript programs for various tasks including printing 'Hello, World!', adding two numbers, calculating the area of a triangle, converting kilometers to miles, and converting Celsius to Fahrenheit. Each section includes example code and explanations of the concepts involved, such as variables, operators, and mathematical functions. The document serves as a practical guide for beginners to understand basic JavaScript programming techniques.

Uploaded by

datalogdigital
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

JavaScript Program To Print Hello

World
1. Using alert()

The alert() method displays an alert box over the current window with the
specified message.
Source Code
// the hello world program
alert("Hello, World!");

2. Using document.write()

document.write() is used when you want to print the content to the HTML
document.
// the hello world program
document.write('Hello, World!');

JavaScript Program to Add Two


Numbers
In this example, you will learn how to add two numbers and display their
sum using various methods in JavaScript.

To understand this example, you should have the knowledge of the


following JavaScript programming topics:
 JavaScript Variables and Constants
 JavaScript Operators
We use the + operator to add two or more numbers.

Example 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);

Example 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}`);

JavaScript Program to Calculate


the Area of a Triangle
In this example, you'll learn to write a program to calculate the area of a
triangle in JavaScript.

To understand this example, you should have the knowledge of the


following JavaScript programming topics:
 JavaScript Operators
 JavaScript Math sqrt()

If you know the base and height of a triangle, you can find the area using
the formula:

area = (base * height) / 2

Example 1: Area When Base and Height is Known


const baseValue = prompt('Enter the base of a triangle: ');
const heightValue = prompt('Enter the height of a triangle: ');

// calculate the area


const areaValue = (baseValue * heightValue) / 2;

console.log(
`The area of the triangle is ${areaValue}`
);

Example 2: Area When All Sides are Known


// JavaScript program to find the area of a triangle

const side1 = parseInt(prompt('Enter side1: '));


const side2 = parseInt(prompt('Enter side2: '));
const side3 = parseInt(prompt('Enter side3: '));

// calculate the semi-perimeter


const s = (side1 + side2 + side3) / 2;

//calculate the area


const areaValue = Math.sqrt(
s * (s - side1) * (s - side2) * (s - side3)
);

console.log(
`The area of the triangle is ${areaValue}`
);

JavaScript Program to Convert


Kilometers to Miles
In this example, you will learn how to convert kilometer values to miles in
JavaScript.

To understand this example, you should have the knowledge of the


following JavaScript programming topics:
 JavaScript Variables and Constants
 JavaScript Operators

We know that 1 kilometer is equal to 0.621371 miles.


So to convert kilometers to miles, you can use the formula:

miles = kilometers * 0.621371

Example 1: 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.`);


Run Code

Output

Enter value in kilometers: 2.2


2.2 kilometers is equal to 1.3670162000000001 miles.

Here, the kilometers variable is used to store the kilometer value from the
user. Then kilometer value is multiplied with factor to convert into miles.

To convert miles to kilometers, you can use formula:

kilometers = miles / factor

Javascript Program to Convert


Celsius to Fahrenheit
In this example, you will learn to convert Celsius value to Fahrenheit in
JavaScript.

To understand this example, you should have the knowledge of the


following JavaScript programming topics:
 JavaScript Variables and Constants
 JavaScript Operators
You can convert the celsius value to fahrenheit by using the formula:

fahrenheit = celsius * 1.8 + 32

Example: 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.`);
Run Code

Output

Enter a celsius value: 55


55 degree celsius is equal to 131 degree fahrenheit.

In the above program, the user enters the celsius value and is stored in
the celsius variable. Then the fahrenheit formula is used to convert celsius
value to fahrenheit.

You might also like