Docsity

JAVA SCRIPT STUDY NOTES, Study notes of Javascript programming

Toggle sidebar
bg1
avaScript Global Variable
AJavaScript global variableis declared outside the function or declared with
window object. It can be accessed from any function.
Let’s see the simple example of global variable in JavaScript.
1. <script>
2. varvalue=50;//globalvariable
3. functiona(){
4. alert(value);
5. }
6. functionb(){
7. alert(value);
8. }
9. </script>
Test it Now
Declaring JavaScript global variable within function
To declare JavaScript global variables inside function, you need to usewindow
object. For example:
1. window.value=90;
Now it can be declared inside any function and can be accessed from any function.
For example:
1. functionm(){
2. window.value=100;//declaringglobalvariablebywindowobject
3. }
4. functionn(){
5. alert(window.value);//accessingglobalvariablefromotherfunction
6. }
Test it Now
Internals of global variable in JavaScript
When you declare a variable outside the function, it is added in the window object
internally. You can access it through window object also. For example:
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download JAVA SCRIPT STUDY NOTES and more Study notes Javascript programming in PDF only on Docsity!

avaScript Global Variable

A JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function. Let’s see the simple example of global variable in JavaScript.

  1. Test it Now

Declaring JavaScript global variable within function

To declare JavaScript global variables inside function, you need to use window object. For example:

  1. window.value= 90 ; Now it can be declared inside any function and can be accessed from any function. For example:
  2. function m(){
  3. window.value= 100 ;//declaring global variable by window object
  4. }
  5. function n(){
  6. alert(window.value);//accessing global variable from other function
  7. } Test it Now

Internals of global variable in JavaScript

When you declare a variable outside the function, it is added in the window object internally. You can access it through window object also. For example:

  1. var value= 50 ;
  2. function a(){
  3. alert(window.value);//accessing global variable
  4. }

Javascript Data Types

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. It can hold any type of values such as numbers, strings etc. For example:
  3. var a= 40 ;//holding number
  4. var b="Rahul";//holding string

JavaScript primitive data types

There are five types of primitive data types in JavaScript. They are as follows:

Data Type Description

String represents sequence of characters e.g. "hello" Number represents numeric values e.g. 100 Boolean represents boolean value either false or true Undefined represents undefined value Null represents null i.e. no value at all

Play Videox

Operator Description Example

  • Addition 10+20 = 30
  • Subtraction 20-10 = 10
  • Multiplication 10*20 = 200 / Division 20/10 = 2 % Modulus (Remainder) 20%10 = 0 ++ Increment var a=10; a++; Now a -- Decrement var a=10; a--; Now a = JavaScript Comparison Operators The JavaScript comparison operator compares the two operands. The comparison operators are as follows:

Operator Description

== Is equal to === Identical (equal and of same type) != Not equal to !== Not Identical

> Greater than >= Greater than or equal to < Less than <= Less than or equal to JavaScript Bitwise Operators The bitwise operators perform bitwise operations on operands. The bitwise operators are as follows:

Operator Description Example

& Bitwise AND (10==20 & 20== | Bitwise OR (10==20 | 20== ^ Bitwise XOR (10==20 ^ 20== ~ Bitwise NOT (~10) = - << Bitwise Left Shift (10<<2) = 40 >> Bitwise Right Shift (10>>2) = 2 >>> Bitwise Right Shift with Zero (10>>>2) = 2 JavaScript Logical Operators The following operators are known as JavaScript logical operators.

Operator Description Example

&& Logical AND (10==20 && 20==33) = false || Logical OR (10==20 || 20==33) = false ! Logical Not !(10==20) = true JavaScript Assignment Operators The following operators are known as JavaScript assignment operators.

JavaScript If statement It evaluates the content only if expression is true. The signature of JavaScript if statement is given below.

  1. if(expression){
  2. //content to be evaluated
  3. }

Flowchart of JavaScript If statement

Let’s see the simple example of if statement in javascript.

Output of the above example

value of a is greater than 10 JavaScript If...else Statement It evaluates the content whether condition is true of false. The syntax of JavaScript if-else statement is given below.

  1. if(expression){

  2. //content to be evaluated if condition is true

  3. }

  4. else{

  5. //content to be evaluated if condition is false

  6. document.write("a is even number");

  7. }

  8. else{

  9. document.write("a is odd number");

  10. }

JavaScript If-else

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

JavaScript If statement

It evaluates the content only if expression is true. The signature of JavaScript if statement is given below.

  1. if(expression){
  2. //content to be evaluated
  3. }

Flowchart of JavaScript If statement

Let’s see the simple example of if statement in javascript.

  1. **

Flowchart of JavaScript If...else statement

Let’s see the example of if-else statement in JavaScript to find out the even or odd number.

  1. **
  2. document.write("a is even number");
  3. }
  4. else{
  5. document.write("a is odd number");
  6. }