×

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.

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 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 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 Time Picker Demo

Time Pickers in JavaScript are lightweight and mobile-friendly controls that let the users enter or select date and time values. These values can be from a pop-up calendar or a...

5 minutes read.

Javascript comment

Introduction In programming languages, comments are a method to include meaningful annotations within the code that the compiler or interpreter will ignore at run time. That is to say, though comments...

7 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 Variable

Variables are used to store values (name="Ram") or expressions (Sum=x+y). Before using of variable first we need to declare it. We use keyword var to declare a variable like this: var name; There are two types of variables: Local Variable ...

1 minute read.

D3.js Tutorial

Introduction of D3.js D3.js is referred to as a JavaScript library that is used to manipulate the documents according to the data. The intensity of D3 is based on web standards...

5 minutes read.

JavaScript setInterval()

On the Window and Worker interfaces, the setInterval() method calls a function or executes a snippet with a specified time delay between each call. This function provides an interval ID that...

4 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 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 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.

Javascript Redirect Button

What is a redirect button? When a button acts as a hyperlink, it is known as a redirect button. On clicking the button, it transfers the user to another page. How to...

3 minutes read.

Javascript input maxlength

Various attributes often accompany the input tag in HTML to change the properties of the input field. Maxlength() is one such attribute. The maxLength attribute defines the maximum number of characters...

2 minutes read.

Javascript Cookies

Cookie is a piece of data which is sent from a website and stored locally by the user's browser. Cookie is needed because HTTP is stateless protocol. Today most of...

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 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 Print

JavaScript help's the functionality using the print function of window object. In JavaScript print function window.print() print the current web page when it executed. We can call function directly using onclick...

1 minute read.

What is Ternary Operator in JavaScript?

In some cases, a ternary operator can be used instead of an if-else expression. A ternary operator examines a condition and then runs a block of code in response to...

4 minutes read.