JavaScript is a very powerful client-side scripting language. JavaScript is used mainly for enhancing the interaction of a user with the webpage. In other words, you can make your webpage more lively and interactive, with the help of JavaScript. JavaScript is also being used widely in game development and Mobile application development.
The language was initially called LiveScript and was later renamed JavaScript. There are many programmers who think that JavaScript and Java are the same. In fact, JavaScript and Java are very much unrelated. Java is a very complex programming language whereas JavaScript is only a scripting language. The syntax of JavaScript is mostly influenced by the programming language C.
You should place all your JavaScript code within < script > tags (< script > and < /script >) if you are keeping your JavaScript code within the HTML document itself. This helps your browser distinguish your JavaScript code from the rest of the code. As there are other client-side scripting languages (Example: VBScript), it is highly recommended that you specify the scripting language you use. You have to use the type attribute within the < script > tag and set its value to text/javascript like this:
< script type="text/javascript"> < /script >
<html> <head> <title> My First JavaScript code!!!</title <script> alert("Hello World!"); </script> </head> <body> </body> </html>
Before using a variable, you first need to declare it. You have to use the keyword var to declare a variable. Declaration makes value of the variable “undefined”.
EX. var name;
You can assign a value to the variable either while declaring the variable or after declaring the variable.
EX. name = "John Doe";
Declaration and initialization may be done in same line.
EX. var name = "John Doe";
Smae as other programming languages.
if(conditon){ //code; } else{ //code; }Can be nested.
switch(expression){ case choice1: //code; break; case choice2: //code; break; default: //code; }
for(initializer; condition; final-expression){ //code; }--- As with language, break and continue can be used with JS loops.
initializer; while(condition){ //code; }
initializer; do{ //code; }while(condition);