Introduction To JavaScript

Introduction

 
JavaScript is a scripting language that enables the enhancement of static web applications. JavaScript gave a proper impact on the visitor's eyes to visit again, as we have seen many times drop-down menus, moving text and changing context, these are all techniques enabled by JavaScript. We can say that JavaScript is the language of choice on the web. JavaScript can even be used outside of the web applications, like administrative tasks, and so on.
 
In this article, we will learn what JavaScript is, how it works, and what we can do with it. In this article, we will also see the basic examples of every topic with proper JavaScript syntax.
 
[Editor's note: Java existed before JavaScript.]
 
The following are the topics covered in this article:
  • What JavaScript is 
  • Why use JavaScript
  • "Hello World" example in JavaScript
  • Variable and DataTypes
  • Operators

What is JavaScript?

 
JavaScript is a scripting language, also an interpreted language. Interpreter means, JavaScript code is converted into machine code, which the computer understands, this conversion happens at the time of the code is run; it must be repeated every time the code is run. JavaScript is used in HTML Pages to make HTML page live. Most of the time when we say JavaScript, most people think that it is Java language, but It is not any relation with Java. It is completely different from a similar naming.
 
JavaScript has a language specification called ECMA Scripting. The most interesting thing is we only write the script (a program in JavaScript is known as a script) and append with the HTML page and it works.  
 

Do's

Don'ts

Make HTML page more interactive Can't help to Read/ Write hard disk data, copy files and call other Programme.
Execute code on events This will only work on the one tab of the browser, it can't affect another tab.
Prevent the page reload using AJAX technology Some other security issues.
Get/Set the Cookies, data and output messages  
And many more.....  
 
Modern JavaScript is a generic language, it does not work on only one browser. These are console programs and servers. Node.JS is the example of that.
 

Why to use JavaScript?

 
The main reason for choosing JavaScript is its widespread use and availability. All the common browsers support JavaScript, so we can say that if we use scripts then we are able to make our website more attractive and lightweight.  JavaScript enhances its ability. It is becoming more and more powerful, trying to reach desktop apps.
 
Client-side validation, special effects in images, text, and any specific area, thousands of other things. JavaScript is also used to change the state. Suppose we click on the button or any link event server gives us an entirely new page to look at, but JavaScript makes it on the client-side. 
 
"Hello World" example in JavaScript
 
The following is a simple code for printing Hello world in the browser.
  1. <!DOCTYPE html>    
  2. <html >    
  3. <head>    
  4.     <title></title>    
  5.     <script type="text/javascript">    
  6.         document.write(" hello world");    
  7.     </script>    
  8. </head>    
  9. <body>    
  10.  </body>    
  11. </html>   
<script>....</script>
 
The <script> tag tells the browser that there is code inside and the type attribute specifies that it is JavaScript code, from HTML5 it is optional.
 
In
 
document.write()
 
The document.write() method is used to display anything inside the browser.
 

Variable and DataTypes

 
Definition of Variable
 
Variables store the values, either permanently or temporarily. The variable is a memory block that we create to do some operation. For example:
 
Var x; // Here x is the name of the variable
x=5;// In this statement "x" variable holds a value 5
 
In JavaScript you can assign to a variable that you haven't defined in a var; For example:
 
x="hello";
 
Technically it will not raise any error but for good practice use the var key word. Rather then Internet Explorer all browsers do not show an error but Internet Explorer shows an auto-generated variable is an internal reference that can't be changed.
 
Variable Names
 
A variable name always starts with a letter, $ or _, Other characters might be a number and all. JavaScript is case-sensitive (Red is different from red).
 
Reserved words
 
In JavaScript, there are also some reserved words that we never use as a variable name. var, function, return, class, implements are some words. class is not used in modern JavaScript but still, it is reserved
 
DataType
 
In JavaScript, there are some basic data types and they are:
  1. Number (it can either be an integer or a non-integer (flaot): 1, 2, 1.5, and so on)
  2. string (a value enclosed in single or double quotes, for example 'rajeev')
  3. boolean (possible values TRUE or FALSE)
Operator
  1. Arithmetic operator
  2. String concatenation
  3. Increment/Decrement ++,--
  4. Bitwise Operator
  5. Logical Operator
  6. Assignment Operator
Arithmetic operator
 
JavaScript has a range of basic mathematical capabilities such as addition, subtraction, multiplication, and division, each of these is represented by a symbol, respectively  +.-.*,/, and so on. Examples:
  • var num1=20;
  • var num2=23;
  • var sum= num1 + num2; 
  • document.write(sum)
    String concatenation
The operator + concatenates string s, as in:
 
var str=  "hello" + " world" ; // hello world
 
If any of its arguments are a string, then another operand is coerced into a string too. Unlike many other languages, there is no difference that an argument is a string, the left one or the right one. In either case, a non-string operand is converted. For example:
 
document.write('1'+2); // "12" will be the output
document.write('2'+2); // "22" will be the output 
 
Increment/Decrement ++,--
 
Like other programming languages (derived from the C language) the increment or decrement operator will also increase or decrease the value by one step forward/ backward. For example:
 
var i=8;
i++; 
alert(i);// output 9 
i-- 
alert(i);// output 8
Bitwise Operator
 
The Bitwise operator treat numbers as signed 32-bit integers. These are rarely used.  The following bitwise operator is supported in JavaScript.
  • AND (&)
  • OR (|)
  • XOR (^)
  • NOT (~)
  • Left shift (<<)
  • Right shift (>>)
  • Zero-fill Right shift (>>>)
Unlike the C language, bitwise operations are not very fast in JavaScript, so we shouldn't use them for low-level optimization. 
 
Logical Operator
  • Logical AND (&&)
  • Logical  OR(||)
  • Logical NOT (!)
For example
 
alert(true && false) // false
alert (!false ) // true
alert (false || true ) // true
 
A logical operator can be applied to do anything. A non-Boolean value is converted into Boolean for the purpose of evaluation.
 
Assignment Operator
 
Assignment operators are used to assigning a value to a variable. The value is assigned always right to left. For example:
 
var x=45;
y+=10; // 55 will be the value y=y+10
var y=x;//  in this code the value of gets assign to y.
 

Summary

 
This article explains the basic concepts of JavaScript, why we use JavaScript, and what is JavaScript. This article explains all the basic concepts of JavaScript from the beginning, JavaScript variables, data types, and operators.