JavaScript Notes

JAVASCRIPT

<button type=”button” onclick=”document.getElementById(‘demo’).innerHTML = ‘Hello JavaScript!'”>

Click Me!</button>

JavaScript can “display” data in different ways:

  • Writing into an alert box, using window.alert().
  • Writing into the HTML output using document.write().
  • Writing into an HTML element, using innerHTML.
  • Writing into the browser console, using console.log().
<script>
window.alert(5 + 6);

document.write(5 + 6);

document.getElementById("demo").innerHTML = 5 + 6;

console.log(5 + 6);
</script>

àJavaScript statements are separated by semicolon.

à Fixed values are called literals. Variable values are called variables.

  • varkeyword tells the browser to create a new variable.
  • Code after double slashes//or between /* and */ is treated as a comment.
  • JavaScript does not interpretVARor Var as the keyword var.
  • ; Semicolons separate JavaScript statements.
  • Javascript ignore the white spaces.
  • Javascript line breaks, best fit from operators.
Keyword Description
Break Terminates a switch or a loop
Continue Jumps out of a loop and starts at the top
debugger Stops the execution of JavaScript, and calls (if available) the debugging function
do … while Executes a block of statements, and repeats the block, while a condition is true
For Marks a block of statements to be executed, as long as a condition is true
function Declares a function
if … else Marks a block of statements to be executed, depending on a condition
return Exits a function
switch Marks a block of statements to be executed, depending on different cases
try … catch Implements error handling to a block of statements
var Declares a variable

 

x = x + 5; // Asignment operator

x == 5; // equal to method

y = "5" + 5; // output will be 55 bcoz double quotes value treated

as a string not int.

var x = 16 + 4 + "Volvo"; // output will be 20Volvo

var x = "Volvo" + 16 + 4; // output will be Volvo164

 

  • the + operator can also be used to concatenate (add) strings.

 

JAVASCRIPT OBJECTS

JavaScript objects are written with curly braces.

Object properties are written as name:value pairs, separated by commas.

<p id="demo"></p>

<script>

var person = {

firstName : "John",

lastName  : "Doe",

age       : 50,

eyeColor  : "blue"

};

document.getElementById("demo").innerHTML =  person.firstName + " is " + person.age + " years old.";

</script>

à JavaScript Typeof Operator to find the type of a JavaScript variable.

<p id="demo"></p>

<script>

document.getElementById("demo").innerHTML =

typeof "john" + "<br>" +

typeof 3.14 + "<br>" +

typeof false + "<br>" +

typeof [1,2,3,4] + "<br>" +

typeof {name:'john', age:34};

</script>

var person; // here the person is undefined value

 

à document.getElementsByTagName() method returns a collection of all elements in the document with the specified tag name, as a NodeList object.

The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.

 

24 MAR 2015

JavaScript Scope

Scope is the set of variables you have access to. In JavaScript, scope is the set of variables, objects, and functions you have access to.

Local JavaScript Variables

Local variables have local scope: They can only be accessed within the function.

function myFunction() {
var carName = “Volvo”;
}

Global JavaScript Variables

A global variable has global scope: All scripts and functions on a web page can access it.

var carName = ” Volvo”;
function myFunction() {
}

àThe lifetime of a JavaScript variable starts when it is declared.

àLocal variables are deleted when the function is completed.

àGlobal variables are deleted when you close the page.

Facebooktwittergoogle_plusredditpinterestlinkedinmail
1 Star2 Stars3 Stars4 Stars5 Stars (9 votes, average: 4.89 out of 5)
Loading...