Saturday, May 12, 2018

JavaScript

JavaScript is an object-based scripting language that is lightweight and cross-platform.
JavaScript is not compiled but translated. The JavaScript Translator (embedded in browser) is responsible to translate the JavaScript code.

JavaScript is used to create interactive websites. It is mainly used for:
  • Client-side validation
  • Dynamic drop-down menus
  • Displaying data and time
  • Displaying popup windows and dialog boxes (like alert dialog box, confirm dialog box and prompt dialog box)
  • Displaying clocks etc.

Javascript example is easy to code. JavaScript provides 3 places to put the JavaScript code: within body tag, within head tag and external JavaScript file.


The script tag specifies that we are using JavaScript.
The text/javascript is the content type that provides information to the browser about the data.
The document.write() function is used to display dynamic content through JavaScript. We will learn about document object in detail later.


3 Places to put JavaScript code

  1. Between the body tag of html
  2. Between the head tag of html
  3. In .js file (external javaScript)

JavaScript variable is simply a name of storage location. There are two types of variables in JavaScript : local variable and global variable.
There are some rules while declaring a JavaScript variable (also known as identifiers).
  1. Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
  2. After first letter we can use digits (0 to 9), for example value1.
  3. JavaScript variables are case sensitive, for example x and X are different variables.

JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function.


JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript.
  1. Primitive data type
  2. Non-primitive (reference) data type
JavaScript is a dynamic type language, means you don't need to specify type of the variable because it is dynamically used by JavaScript engine. You need to use var here to specify the data type.


The JavaScript if-else statement is used to execute the code whether condition is true or false. There are three forms of if statement in JavaScript.
  1. If Statement
  2. If else statement
  3. if else if statement

No comments:

Post a Comment