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:







on Leave a Comment

JavaScript Variables

As other programming languages, JavaScript also supports data types and variables. Variables are piece of memory blocks in which we can store data (values). And data types decides what type of data can be stored in variables and what operations on variable can be performed.

JavaScript supports following data types:

Number, e.g. 100, 105.5, 10.0 etc.
String, e.g. 'abc', "xyz" etc.
Boolean, e.g. true and false 
Null and Undefined
Object (composite data type)

JavaScript represents numbers as 64-bit floating-point format defined by the IEEE 754 standard.

Example of JavaScript Variable

var keyword is used to declare variables in JavaScript

<script type="text/javascript">
      var a = 10;
      var b = 20;
      var c = a + b;
</script>

In above example, we define three variable using var keyword namely a, b and c. Variables a and b stores 10 and 20 respectively. And sum of a and b is assigned to variable c.

JavaScript Identifiers

All names are Identifiers. A variable must have unique name.

- Reserved words cannot be used as a variables name.
- Variables name should not start with a number like 0, 1, 2.
- Variable name should start with a alphabets or underscore like abc, _abc.
- All variable names are case sensitive.