on Leave a 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.








on Leave a Comment

Introduction to Javascript

Javascript is a client side scripting language. It is a primary used language on the web. It is lightweight and interpreted language. Javascript has ability to change HTML content. Today all modern web browsers support javascript. 

Javascript is developed by Netscape Communication at 1995. Javascript was first known as liveScript and then it changed to Javascript, may be because of popularity of Java. 

Advantage of Javascript

It is client side scripting language. Javascript code runs by the user processer and thus it saves bandwidth interaction with server.

Javascript is easy to learn and it’s much look like English language. 

Javascript has plenty of predefined function, variable thus provides greater functionality to the user.

Using javascript you can create interactive web pages that catches attraction of site visitor.

It is fast because it runs on user system and its need not be process on web server.

Limitations of Javascript

Javascript code can be used by hackers to infect user's system, so it is security issue. Modern web browsers set some restriction but still, it’s not fully secure.

Using javascript, you can't work with files on server because it is a client side scripting language.

Javascript doesn't support multithreading.

Javscript depends on browser, so it is interpreted differently on different browsers.

All browsers has option to disable javascript, so user can disable javascript. 



on Leave a Comment

Java Program to Find Duplicate Characters in a String

This java program finds duplicate characters in a string. In this program, we assign a string to a variable named "str" and this string is converted to character array using toCharArray() method and assigned to a variable "c". Then using nested for loop, we compare each character of char array to the other characters in array and if they match, we print that character.

Java Program to Find Duplicate Characters in a String

class FindDuplicateString{
 public static void main(String[] args){
  String str = "java";
  char[] c = str.toCharArray();
  System.out.print("Duplicate characters : ");
  for(int i=0; i<str.length(); i++){
   for(int j=i+1; j<str.length(); j++){
    if(c[i] == c[j]){
              System.out.print(c[i]+" ");         }
   }   
  }
 }
}

Output:

Duplicate characters : a



on Leave a Comment

Java Program to Convert String to Int

In java, we can convert string to integer using static methods like parseInt() and valueOf(). 

Java String to Int Using Integer.parseInt()

parseInt() is a static method of Integer class, this method returns a primitive int.

class StringToInt{
 public static void main(String[] args){
  String str="100";
  
  //String to Int using parseInt method
  int a = Integer.parseInt(str);
  System.out.println("Sum is "+(a+10));
 }
}

Output:

Sum is 110

Java String to Int Using Integer.valueOf()

valueOf() method returns a Integer object.

class StringToInt{
 public static void main(String[] args){
  String str="100";
  
  //String to Int using parseInt method
  Integer a = Integer.valueOf(str);
  System.out.println("Sum is "+(a+10));
 }
}

Output:

Sum is 110