JavaScript Basics

Introduction

In this article, we will learn about JavaScript, its basic terms, and syntaxes with example programs.

JavaScript

Javascript is the first scripting programming language that supports browsers and the most popular scripts. JavaScript is the programming language of the Web, and it is an interpreted programming language. Interpreter means converting the JavaScript code into whichever computer language the machine knows; this conversion happens when the code is run, and it must be repeated once the code is run.  

JavaScript is used to make HTML Pages live within HTML Pages. The name JavaScript is similar to Java, but it is totally different from Java Programming Language, and in fact,  Java was in use before the JavaScript Language.

History of JavaScript

JavaScript was created in May 1995 by Brendan Eich at Netscape, and in 10 days, the first version of JavaScript was ready. The original name of JavaScript was Mocha, named by the founder of Netscape.

In September 1995, the name of JavaScript was changed to Live Script from Mocha. Again in December 1995, the name changed from LiveScript to JavaScript. 

Features of JavaScript

  • JavaScript is a lightweight language.
  • JavaScript is a case-sensitive and object-based scripting language.
  • JavaScript is an untyped language, which means we don't need to define the data type of the variables.
  • Gives the user more control over the browser.
  • Every statement in Javascript terminates with a semicolon (;).
  • Most of the Javascript control statements syntax is the same as the syntax of control statements in C and Java language.
  • JavaScript has built-in functions to determine the date and time. 

Why do we use JavaScript?

JavaScript is used in web pages to add functionality, and validation forms, communicate with the server, and read-write HTML elements. JavaScript is a client-side scripting language. All the browsers support JavaScript language, and it makes our website more attractive and lightweight. JavaScript enhances the efficiency of our websites.

JavaScript is often used for modifying the state. Suppose we're clicking on the button, or some connection event server will give us a whole new page to look at, but JavaScript will do it on the client side. 

Advantages of JavaScript

  • Make the HTML page more interactive.   
  • JavaScript is very easy to learn and understand.
  • JavaScript increases the performance of websites and web applications by raising the length of code 
  • Execute code on events.   
  • Get/Set the Cookies, data, and output messages.    
  • All common browsers support JavaScript. So it is very popular.
  • JavaScript is client-side, so it reduces the demand on servers overall.

Disadvantages of JavaScript

  • Can't help to Read/ Write hard disk data, copy files, and call other Programmes.
  • This will only work on one tab of the browser. It can't affect another tab.
  • JavaScript supports all browsers, so sometimes different browsers interpret JavaScript code differently.
  • JavaScript does not support multiple inheritances.
  • A single code error will stop all JavaScript code from running on the website.

JavaScript vs Java
 

Comparison JavaScript Java
Oops Javascript is specifically an object-oriented Scripting language. Java is an object-oriented programming language.
Running platform Javascript runs on a web browser and doesn't need any initial setup. Java program and application runs on JVM(Java Virtual Machine), which requires JDK and JRE on a system.
Compilation Javascript is interpreted as it is a scripting language which is a plain text code. Java code is compiled and interpreted as it is a programming language.
Typed Javascript is a weakly typed language Java is a strongly typed language.
Syntax Javascript language syntax is similar to C language, but the naming convention is similar to Java Programming language. Java language syntaxes are similar to C and C++ languages. Java programs will be in class, and objects
File extension JavaScript file has the file extension “.js” and it is interpreted but not compiled. Every browser has a Javascript interpreter to execute JS code. Java program has the file extension “.Java” and translates source code into bytecodes which are executed by JVM(Java Virtual Machine).
Supportive languages Javascript is contained within a web page and integrates with its HTML content. Java is a Standalone language.
Memory JavaScript requires less memory therefore, it is used in web pages. Java program uses more memory.
Hello World Program <!DOCTYPE HTML>
<html>
<body>
  <p>Before the script...</p>
  <script>
    alert('Hello, world!');
  </script>
  <p>...After the script.</p>
</body>
</html>
class A {
    public static void main(String args[]) {
        System.out.println("Hello World");
    }
}

The complete "Hello World" example program in JavaScript is listed below.

<!DOCTYPE html>
<html>
<body>
    <h2>First Web Page in JavaScript</h2>
    <p>First paragraph.</p>
    <p>JavaScript code starts here....</p>
    <script>
        document.write(5 + 6);
    </script>
    <p>JavaScript code ends here......</p>
</body>
</html>

The above program generates the following output.

hello_world

Explanation

In the above JavaScript code, we use internal JavaScript. In this, first, we print a heading in the <h2>tag, then 2 paragraphs into the <p> tag. After that, we use JavaScript code which is started from <script> tag. Inside this, we use the document and write a statement to print the addition of the given numbers. We got 11 as a result. In the last paragraph, the <p> tag prints that the JavaScript code ends.

Object-based Scripting language JavaScript

JavaScript is an Object-based scripting language. But JavaScript doesn't support all OOP functions. In JavaScript, we use object, class, encapsulation, aggregation, and polymorphism -- the OOPs concept. JavaScript only supports single inheritance; it does not support multiple inheritances.

Summary

In this article, we learned about the basic concepts, advantages, and disadvantages of JavaScript and why we use JavaScript with an example program.


Similar Articles