0% found this document useful (0 votes)
40 views

CSS Lec-5 Data Types and Operator.92e7919

The document discusses various JavaScript data types, variables, and operators. It explains that variables can hold different data types like numbers, strings, arrays, and objects. It also describes different types of operators in JavaScript like arithmetic, comparison, logical, assignment, conditional, and bitwise operators. Local and global variables are defined, and it provides examples of using various operators in JavaScript code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

CSS Lec-5 Data Types and Operator.92e7919

The document discusses various JavaScript data types, variables, and operators. It explains that variables can hold different data types like numbers, strings, arrays, and objects. It also describes different types of operators in JavaScript like arithmetic, comparison, logical, assignment, conditional, and bitwise operators. Local and global variables are defined, and it provides examples of using various operators in JavaScript code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

CSS Lecture-5
Topic: Data Type, Variable and Operator

DataTypes and Variable:


 A variable in javascript is simply a name of storage location.
 It is the basic unit of storage in a program.

 A JavaScript variable declaration statement often starts with a keyword.


 The var keyword tells the browser to create a new variable:
var x = 5;
var y = 234.45;

 JavaScript variables can hold many types of data: numbers, text strings, arrays,
objects and much more: For Example

var length = 16; // Number assigned by a number literal


var lastName = "Johnson"; // String assigned by a string literal

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


// Array assigned by an array literal

Course: ClientSideScripting, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

var person = {firstName:”John”, lastName:”Doe”};


// Object assigned by an object literal

 Besides regular numbers, there are so-called “special numeric values”


which also belong to this data type: Infinity, -Infinity and NaN.

 Infinity represents the mathematical Infinity ∞. It is a special value that’s


greater than any number. We can get it as a result of division by zero:

 About NaN, If any expression does not generate any number then
JavaScript result is NaN. For Example: “ABC”/10 will result in NaN

Javascript local variable


A variable which is declared inside block or function is called local variable.
It is accessible within the function or block only. For example:

<script>
function abc(){
var x=10;//local variable
}
</script>
Or,

<script>
if(10<13){
var y=20;//javascript local variable
}
</script>

Course: ClientSideScripting, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

Javascript global variable


A global variable is accessible from any function. A variable i.e. declared
outside the function or declared with window object is known as global
variable. For example:

<script>
var data=200;//gloabal variable
function a(){
document.write(data);
}
function b(){
document.write(data);
}
</script>

Assigning Values to Undeclared JavaScript Variables


If you assign a value to a variable that has not yet been declared, the variable will
automatically be declared as a GLOBALvariable.

This statement:
carName = "Volvo";

will declare the variable carName as a global variable , even if it is executed inside
a function.

Course: ClientSideScripting, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

Operators in JavaScript :-
JavaScript operators are symbols which are used to assign values, compare
values, perform arithmetic operations, and more.

JavaScript supports the following types of operators.


• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Conditional (or ternary) Operators
• String Operators
• typeOf Operators
• Bitwise Operators

Arithmetic Operators :
Arithmetic operators perform arithmetic operations on numbers.

Operator Description
+ Addition
– Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
++ Increment
-- Decrement

Course: ClientSideScripting, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

Relational or Comparision Operators in Java Script :


 Comparison and Logical operators are used to test for true or false.

 Comparison operators are used in logical statements to determine equality


or difference between variables or values.

 Given that x = 5, the table below explains the comparison operators:

Operator Description Comparing Returns


== equal to x == 8 False
x == 5 True
x == “5” True
=== equal value and equal x === 5 True
type x === “5” False

!= not equal x != 8 True

!= = not equal value or not x !== 5 False


equal type x !== “5” true
x !== 8 true
> greater than x>8 False
< less than x<8 True
>= greater than or equal x >= 8 False
to
<= less than or equal to x <= 8 True

Course: ClientSideScripting, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

JavaScript Logical Operators :


 Comparison and Logical operators are used to test for true or false.

 Logical operators are used to determine the logic between variables or


values.

 Given that x = 6 and y = 3, the table below explains the logical operators:

Operator Description Example


(x < 10 && y > 1) is
&& And
true

(x == 5 || y == 5) is
|| Or
false
! not !(x == y) is true

JavaScript Conditional (Ternary) Operator :-


 JavaScript also contains a conditional operator that assigns a value to a
variable based on some condition.

Syntax: variablename=(condition)?value1:value2;

Course: ClientSideScripting, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260

JavaScript Type Operators :-


Operator Description
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an
object type

For Example: typeof 5; // Number


typeof “5”; // string

String str = new String(“Hello”);


str instanceof String // true

JavaScript Bitwise Operators :-


 Bit operators work on bits numbers.
 Any numeric operand in the operation is converted into a bit number.
 The result is converted back to a JavaScript number.

Operator Description Example Same as Result Decimal

& AND 5&1 0101 & 0001 0001 1

| OR 5|1 0101 | 0001 0101 5

~ NOT ~5 ~0101 1010 10

^ XOR 5^1 0101 ^ 0001 0100 4

<< Zero fill left shift 5 << 1 0101 << 1 1010 10

>> Signed right shift 5 >> 1 0101 >> 1 0010 2

Zero fill right


>>> shift 5 >>> 1 0101 >>> 1 0010 2

Course: ClientSideScripting, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like