JavaScript

Java Script

Java Script

JavaScript is the most popular programming language in the world.JavaScript and Java are completely different languages, both in concept and design. JavaScript was invented by Brendan Eich in 1995, and became an ECMA standard in 1997.ECMA-262 is the official name. ECMAScript 6 (released in June 2015) is the latest official version of JavaScript. JavaScript can be placed in the body and the head sections of an HTML page. It is a good idea to place scripts at the bottom of the body element. This can improve page load, because HTML display is not blocked by scripts loading.The type attribute is not required. JavaScript is the default scripting language in HTML.A computer program is a list of "instructions" to be "executed" by the computer.In a programming language, these program instructions are called statements. JavaScript is a programming language.

Placing JavaScripts in external files has some advantages:

  • It separates HTML and code
  • It makes HTML and JavaScript easier to read and maintain
  • Cached JavaScript files can speed up page loads

JavaScript Can Change HTML Content

document.getElementById('demo').innerHTML = 'Hello JavaScript!'

JavaScript Can Change HTML Attributes

 function changeImage() {
     var image = document.getElementById('myImage');
     if (image.src.match("bulbon")) {
         image.src = "pic_bulboff.gif";
     } else {
         image.src = "pic_bulbon.gif";
     }
 }
 

JavaScript Can Change HTML Styles (CSS)

 function myFunction() {
     var x = document.getElementById("demo");
     x.style.fontSize = "25px";           
     x.style.color = "red"; 
 }
 

JavaScript Can Validate Data

 function myFunction() {
     var x, text;
     // Get the value of the input field with id="numb"
     x = document.getElementById("numb").value;
     // If x is Not a Number or less than one or greater than 10
     if (isNaN(x) || x < 1 || x > 10) {
         text = "Input not valid";
     } else {
         text = "Input OK";
     }
     document.getElementById("demo").innerHTML = text;
 }
 

JavaScript Display Possibilities

JavaScript can "display" data in different ways:

  • Writing into an alert box, using window.alert().
  • Writing into the HTML output using document.write().Using document.write() after an HTML document is fully loaded, will delete all existing HTML
  • Writing into an HTML element, using innerHTML.
  • Writing into the browser console, using console.log().

A variable declared without a value will have the value undefined.When used on strings, the + operator is called the concatenation operator.The += assignment operator can also be used to add (concatenate) strings. The numbers (in an arithmetic operation) are called operands. The operation (to be performed between the two operands) is defined by an operator.

JavaScript variables can hold many data types: String, Number, Boolean, Array, Object. JavaScript has dynamic types. This means that the same variable can be used as different types.

 
  var x;               // Now x is undefined
  var x = 5;           // Now x is a Number
  var x = "John";      // Now x is a String
 

JavaScript arrays are written with square brackets.

  var cars = ["Saab", "Volvo", "BMW"]; 
 

JavaScript objects are written with curly braces.

  var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
 

You can use the JavaScript typeof operator to find the type of a JavaScript variable

  typeof "John"                // Returns string
  typeof 3.14                  // Returns number
  typeof false                 // Returns boolean
  typeof [1,2,3,4]             // Returns object
  typeof {name:'John', age:34} // Returns object
 

In JavaScript, a variable without a value, has the value undefined. The typeof is also undefined.Any variable can be emptied, by setting the value to undefined. The type will also be undefined.

   var person;    // Value is undefined, type is undefined
   person = undefined; // Value is undefined, type is undefined
 

In JavaScript null is "nothing". It is supposed to be something that doesn't exist.Unfortunately, in JavaScript, the data type of null is an object. You can consider it a bug in JavaScript that typeof null is an object. It should be null.You can empty an object by setting it to null

  var person = null; // Value is null, but type is still an object
 

Difference Between Undefined and Null

  typeof undefined             // undefined
  typeof null                  // object
  null === undefined           // false
  null == undefined            // true
 

0 comments:

Post a Comment

Search This Blog

Powered by Blogger.