×

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 Window Object

The window object represents a window in browser. An object of window is created automatically by the browser. Window is the object of browser, it is not the object of JavaScript. The...

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

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

Javascript Find Object In Array

What is find() method? It returns the value of the first element in the given array that fulfils the given testing function. However, if no values from the array are able...

3 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 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 setTimeout()

The setTimeout() method creates a timer that, when it expires, runs a function or provides a piece of code. setTimeout() is an asynchronous function, which means that it will not interrupt...

3 minutes read.

Callback and Callback Hell in JavaScript

The widely used client-side programming language JavaScript is simple, light, and interpretive. JavaScript is used by the majority of client-side web applications. It is also possible to use JavaScript on...

3 minutes read.

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

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

Synchronous and Asynchronous in JavaScript

JavaScript is a powerful and versatile language that is used all over the web, from small websites to large applications. It is essential for developers to understand the differences between...

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

JavaScript p5.js

Introduction to p5.js P5.js is a JavaScript library for creative programming. It is built on Processing, a coding environment for creativity. Processing's major goal is to make it as simple as...

4 minutes read.

Javascript Operators

The concept of operators is the basis of nearly all programming languages used in today's computing systems. Operators facilitate the execution of certain functions. For instance, the (+) symbol is...

8 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 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 Console log Multiple variables

What does console.log() do in JavaScript? The console.log() is a function in JavaScript language. It is used to: Print any message in the console which is visible to the userPrint the values...

3 minutes read.