Showing posts with label Writing Your First JavaScript Program. Show all posts
Showing posts with label Writing Your First JavaScript Program. Show all posts
on 1 comment

JavaScript Program to Display Hello World

In JavaScript, you can display any string using alert box. JavaScript alert is a method of window object. But we can also it explicitly without reference to window object. There are many other ways in JavaScript to display string like console.log method.

JavaScript Program to Display Hello World using Alert Box

<!doctype html>
<html>
  <head>
      <meta charset="utf-8">
      <title>Hello World</title>
  </head>
  <body>
      <button onclick="print()">Click Me...</button>
  </body>
  <script>
      function print(){
          alert("Hello World...");
      }
  </script>
</html>

Output:



When you click on this button, a pop up box occurs that displaying "Hello World...".

JavaScript Program to Display Hello World using console.log

console.log method is used to print string on console window. If you use this method nothing will display on the screen, but it will display on console window.

<!doctype html>
<html>
  <head>
      <meta charset="utf-8">
      <title>Hello World</title>
  </head>
  <body>
      <button onclick="print()">Click Me...</button>
  </body>
  <script>
      function disp(){
          console.log("Hello World...");
      }
  </script>
</html>

Output: