×

Javascript Example

What is JavaScript?

JavaScript is a highly versatile programming language. It provides the capability to add interactivity to static web pages, which are designed with HTML and CSS. It also provides the capability to use dynamic styles, dynamic removal or addition of HTML elements, and so much more. For instance, when a user attempts to submit an empty form and receives an error message, it is JavaScript that provides the capability.

JavaScript is the language of web browsers. When we write JavaScript code, the browser runs it. But if we want to use JavaScript outside the browser environment, we need Node.js, which is the runtime environment of JavaScript. This enables the development of server-side applications.

JavaScript can therefore be utilized to make the front-end of web applications interactive while also being able to make it possible to develop servers to manage back-end tasks with Node.js. This JavaScript tutorial provides in-depth information about the language.

JavaScript is primarily utilized for web-based applications, but it can be utilized for hardware control implementations as well. Some of the applications of JavaScript are listed below:

Making web pages interactive and improving performance on web pages.

  1. Making web pages interactive for users with varied functionalities, such as: Displaying or hiding extra information or user information by a click on a button.
  2. Altering the color of a button when the mouse hovers over it.
  3. Creating a sliding gallery of images on the home page.
  4. Creating zoom-in and zoom-out functionality for images.
  5. Running timers and countdowns on a web page.
  6. Adding animations.
  7. Utilizing interactive drop-down menus.
  8. Playing audio and video content on a web page.

Development of web and mobile applications

Developers have the freedom of using different JavaScript frameworks to create web and mobile applications. The frameworks consist of numerous JavaScript code libraries that enable developers to code based on specific specifications. Some client technologies for developing the front-end user interface are Angular and React Native. The majority of organizations use JavaScript code libraries, particularly Node.js, which is a JavaScript runtime environment that is built on the JavaScript V8 engine.

Development of web server applications:

JavaScript can be utilized to create basic web servers and can be supplemented with back-end infrastructure by using Node.js.

Game development

JavaScript can be utilized to create and design games that run in web browsers.

Most Popular Examples of JavaScript

Example 1: JavaScript Program to Print Hello World

<html> 

<body> 

<script type="text/javascript"> 

 alert("Hello World"); 

</script> 

</body> 

</html> 

Output

JavaScript Examples

Example 2: JavaScript Program to Find the Factorial of a Number

<!DOCTYPE html>

<html>

<head>

</head>

<body style = "text-align: center; font-size: 20px;">

<h2> Welcome to the JavaScript </h2>

Enter a particular number: <input id = "num">

<br><br>

<button onclick = "fact()"> Please type any Factorial number </button>

<p id = "res"></p>

<script>

function fact(){

var i, num, f;

f = 1;

num = document.getElementById("num").value;

for(i = 1; i <= num; i++) 

{

f = f * i;

}

i = i - 1; 

document.getElementById("res").innerHTML = "The factorial of the number " + i + " is: " + f ;

}

</script>

</body>

</html>

Output

JavaScript Examples

Example 3: JavaScript Program to Format the Date With Expected Output

//mm-dd-yyyy, mm/dd/yyyy or dd-mm-yyyy, dd/mm/yyyy

var today = new Date();

var dd = today.getDate();

var mm = today.getMonth()+1;

var yyyy = today.getFullYear();

if(dd<10)

{

    dd='0'+dd;

}

if(mm<10)

{

    mm='0'+mm;

}

today = mm+'-'+dd+'-'+yyyy;

console.log(today);

today = mm+'/'+dd+'/'+yyyy;

console.log(today);

today = dd+'-'+mm+'-'+yyyy;

console.log(today);

today = dd+'/'+mm+'/'+yyyy;

console.log(today);

Output

JavaScript Examples

Example 4: JS Form Program Example

<!DOCTYPE html>

<html>

<head>

<script>

function validateForm() {

  let x = document.forms["myForm"]["fname"].value;

  if (x == "") {

    alert("Please enter your Name");

    return false;

  }

}

</script>

</head>

<body>

<h2>JavaScript Test Validation</h2>

<form name="myForm" action="/action_page.php" onsubmit="return validateForm()" method="post">

  Enter Name: <input type="text" name="fname">

  <input type="submit" value="Submit">

</form>

</body>

</html>

Output

JavaScript Examples
JavaScript Examples

Example 5: POPUP Message Program Using Event

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Confirm Box</h2>

<button onclick="myFunction()">Please Try it</button>

<p id="Test Confirm Box"></p>

<script>

function myFunction() {

  var txt;

  if (confirm("Please Press a button!")) {

    txt = "You pressed Button!";

  } else {

    txt = "You pressed Cancel Button!";

  }

  document.getElementById("Test Confirm Box").innerHTML = txt;

}

</script>

</body>

</html>

Output

JavaScript Examples
JavaScript Examples
JavaScript Examples

Example 6: Display Alert for Prompt Message Program

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Prompt Example</h2>

<button onclick="myFunction()">Please Try for Prompt message</button>

<p id="Prompt Example"></p>

<script>

function myFunction() {

  let text;

  let user = prompt("Please enter your name:", "Your First Name");

  if (user == null || user == "") {

    text = "User cancelled the prompt.";

  } else {

    text = "Hello " + person + "! How are you?";

  }

  document.getElementById("Prompt Example").innerHTML = text;

}

</script>

</body>

</html>

Output

JavaScript Examples
JavaScript Examples

Example 7: Line-Breaks Pop-Up Message

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript</h2>

<p>Line-breaks Example in a popup box.</p>

<button onclick="alert('Hello
How are you?')">Please Try for line-breaks Example</button>

</body>

</html>

Output

JavaScript Examples
JavaScript Examples

Conclusion

In conclusion, JavaScript stands out as a flexible programming language that plays a crucial role in contemporary web development. It allows developers to build dynamic and interactive user interfaces by utilizing features such as DOM manipulation, user input handling, and executing code right in the web browser.


Related Topics

Javascript Document Object

Document object represents the whole HTML documents. When HTML document is loaded in browser it becomes the document object. It is the root elements that represent the HTML documents. With the help of...

2 minutes read.

JavaScript dropdown onchange

What is onchange event? It is an event in JavaScript used to make the web pages dynamic. The onchange event gets triggered whenever the value of an event changes. It usually...

3 minutes read.

Javascript Re-Password Validation

Example <!DOCTYPE html>   <html>   <head>   <script>   function pass_validation()   {   var firstpassword=document.f1.password1.value;     var secondpassword=document.f1.password2.value;     if(firstpassword==secondpassword){     return true;     }     else{   alert("Password does Not Match");     return false;     }     }    </script>   </head>   <body>   <form name="f1" action="/JavaScript/Index" onsubmit="return pass_validation()">   Password:<input type="password" name="password1" /><br/>   Re-enter :<input type="password" name="password2"/><br/>   <input type="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

1 minute read.

JavaScript Image resize before upload

Image resizing can be a quite tedious task, so it is usually done on the server-side. The resized image file is then delivered to the client side. However, sometimes the...

3 minutes read.

Javascript Date

In JavaScript Date Object is data type build into the JavaScript language. Data object are created with the new Date(). JavaScript Date instance that represents a single moment in time. JavaScript...

2 minutes read.

Javascript Email Validation

We can validate the email with the help of JavaScript. Here we check the condition related to any email id, like email it must have "@" and "." sign and...

2 minutes read.

Minify JavaScript

What do you mean by the term ‘Minification’? The technique of minification reduces the amount of code and markup in your websites and scripting files. It's one of the most used...

4 minutes read.

JavaScript Function

An important part of JavaScript is the ability to create new functions within <script>.....</script> tag. To declare a function in JavaScript using function keyword. We call JavaScript function multiple times to reuse the...

3 minutes read.

Javascript Forms Validation

Form validation works at server, after client had entered all the necessary details and then pressed submit button. JavaScript provides the facility to validate the form on the client side...

9 minutes read.

Javascript Math

Math is build-in object that has properties and methods for mathematical constants and functions. It allows you to perform mathematical tasks on numbers. Syntax varpi_val = Math.PI; varsin_val = Math.sin(30); Examples Math.pow() Math.pow(x,y) returns the value of x to the power...

2 minutes read.

Functional Programming in JavaScript

Most developers work with object-oriented programming while developing their applications, but sometimes they need to change their approach to solve some specific tasks. Although mostly we tend to go for...

6 minutes read.

JavaScript redirect URL with parameters

Data can be often passed between web pages as information in URL parameters or query strings. A URL (Uniform Resource Locator) can have a single parameter or multiple parameters.Parameters can...

4 minutes read.

Array to String in JavaScript

As a web developer, you often have to deal with multiple data types in a single application. How to convert an array to a string in JavaScript is essential for...

19 minutes read.

Javascript Number

The Number object is a wrapper object which allows you to work with numerical values. The number object is created using the Number() constructor.It may be integer or floating-point. Syntax var n=new Number(value); Examples <!DOCTYPE html>   <html>   <body>   <script>   var x=100;//integer value   var y=100.7;//floating point value   var z=12e5;//exponent value, output: 1200000   var n=new Number(20);//integer value by number object     document.write(x+" "+y+" "+z+" "+n);   </script>   </body>   </html> Try Now Output 100 100.7 1200000 20 Description The primary...

1 minute read.

Javascript Page Redirect

The window.location object can be used to get the current page address (URL) and to redirect browser to new page. In some websites when we visit, we face a situation where we...

3 minutes read.

Javascript If Else

Introduction The if-else statement is an important control structure for decision-making in code based on some conditions. It gives programmers the ability to add decision-making functionality into their programs. Using if-else,...

6 minutes read.

Javascript Dialog Box

A JavaScript dialog box is predefined function which is used to perform different task. Some functions are used in JavaScript Dialog box. FunctionDescriptionalert()To give alert message to userprompt()To input value from...

2 minutes read.

Javascript Event

Events are things that happen, usually user action, that are associated with an object. All object have properties and methods. Some objects also have events. The event handler is a command that is...

1 minute read.

Javascript Object

An object is an entity having state and behavior. JavaScript is an object oriented scripting language. JavaScript is template based not class based but we can create object directly. Syntax to...

2 minutes read.

JavaScript focus on input

In JavaScript, the elements can be focused using either focus() method or onfocus() event. What is focus() method? When we want to focus on an element (if it can be focused) or...

4 minutes read.