You are on page 1of 5

The typeof operator returns the type of a variable or an Difference Between Undefined and Null

expression.
Undefined and null are equal in value but different in type.
Example 23
Example 28
typeof “” // Returns “string”
typeof undefined // undefined
typeof “Rithika” // Returns “string”
typeof null // object
typeof “Harini Kumar” // Returns “string”
null === undefined // false
typeof0 // Returns “number”
null == undefined // true
typeof81 // Returns “number”
typeof8.14 // Returns “number” Primitive Data
typeof(3+2) // Returns “number”
A primitive data value is a single simple data value with no
additional properties and methods. The typeof operator
Undefined
can return one of these primitive types.
In JavaScript, a variable without a value, has the value • string
undefined. The typeof is also undefined.
• number
Example 24
• boolean
var bike; // Value is undefined, type is
• undefined
undefined
Example 29
Note : Any variable can be emptied, by setting
the value to undefined. The type will also be typeof “Rajesh” // Returns “string”
undefined.
typeof 1.44 // Returns “number”
Empty Values typeof true // Returns “boolean”
An empty value has nothing to do with undefined. An typeof false // Returns “boolean”
empty string has both a legal value and a type. typeof a // if a has no value, it returns
Example 25 “undefined”

var bike = “”; // The value is “”, the typeof Complex Data
is “string”
The typeof operator can return one of two complex types:
Null
• function·
In JavaScript null is “nothing”. It is supposed to be
something that doesn’t exist. In JavaScript, the data type • object
of null is an object. You can empty an object by setting it
to null. The type of operator returns object for both objects, arrays
and null. It does not return object for functions.
Example 26
Example 30
var personName = {firstName:”Harini”,last Name;
”Kumar”, age:13, height:”155 cms”}; typeof {name, ‘Karthik’, age 27} // Returns “object”
personName = null; //Now value in null, but typeof [10, 20, 30, 40, 50] // Returns “object”
type is still an object (not “array”, see
You can also empty an object by setting it to undefined: note below)
typeof null // Returns “object”
Example 27
typeof function sampleFunc() { } // Returns “function”
var personName = {firstName:”Harini”, lastName:
”Kumar”, age:13, height:”155 cms”}; Note: The typeof operator returns “object” for
arrays because in JavaScript arrays are
personName = undefined; // Now both value and objects.
type is undefined.
IT & ITES : COPA (NSQF Level - 4) - Related Theory for Exercise 2.1.96A 9

Copyright Free under CC BY Licence


IT & ITES Related Theory for Exercise 2.1.96B
COPA - JavaScript and creating Web page

Using JavaScript Constants and Operators


Objectives : At the end of this lesson you shall be able to
• explain constants in JavaScript
• explain operators in JavaScript.

Constants bike.color = “grey”;


// You can add a property:
Constants are a special kind of variable, store a value that
never changes during the course of the program. bike.owner = “Sree”;

The syntax to create a Constant is. But you can NOT reassign a constant object.

const CONSTANT_NAME:DataType = value; Example 4

In the above syntax, “const” is the special keyword, const bike = {type:”Yamaha”, model:”R15", color:”blue”};
reserved to define a constant. As you can see, this syntax
looks a lot like a variable declaration but with the var bike = {type:”Tvs”, model:”Star city”, color:”black”};
keyword replaced with “const”. Most programmers use all
caps for the name of the constants to differentiate them // ERROR
from variables. Constant Arrays can Change

Example 1 You can change the elements of a constant array.

const FRIEND = ‘Shanthi’; Example 5


const BROTHER_AGE = 46;
// You can create a constant array:
Note :The keyword const is a little misleading. constant bikes = [“TVS”, “Yamaha”, “Royal
It does NOT define a constant value. It defines Enfield”];
a constant reference to a value.Because of this,
we cannot change constant primitive values, // You can change an element;
but we can change the properties of constant bikes[0] = “suzuki”;
objects.
bikes.push (“Bajaj”); //you can add an element
Primitive Values But you can NOT reassign a constant array:
If we assign a primitive value to a constant, we cannot Example 6
change the primitive value.
const bikes = {“TVS”, “Yamaha”, “Royal Enfield”];
Example 2
bikes = [“TVS”, “Yamaha”, Bajaj”]; //ERROR
const PI = 3.141592653589793;
Operators
PI = 3.14; // This will give an error
There are eight types of operators in JavaScript. These
PI = PI + 10; // This will also give an are
error
• Additive Operators
Constant Objects can Change
• Multiplicative Operators
Change the properties of a constant object.
• Bitwise operator
Example 3 • Equality operator
// You can create a const object: • Relational Operator
const bike = {type. “Yamaha”, model. “R15”, color. • Unary Operators
“blue”};
• Ternary Operator
// You can change a property:
• Assignment Operators

10

Copyright Free under CC BY Licence


Additive Operators: The term additive operators include Example10
both addition (+) and subtraction( -) as subtraction is also
addition with a negative number. Operation Result Same as Result
Example 7 5&1 1 0101 & 0001 0001

32+67; // this is 99 5|1 5 0101 | 0001 0101

d+e; // Adds d with e ~5 10 ~0101 1010

3-7; // return -4 5 << 1 10 0101 << 1 1010

Sometimes JavaScript addition can results in unexpected 5^1 4 0101 ^ 0001 0100
results. 5 >> 1 2 0101 >> 1 0010
Example 8 5 >>> 1 2 0101 >>> 1 0010

var num1="Runs"; Note: The examples above uses 4 bits unsigned


var num2=784; binary numbers. Because of this ~ 5 returns
10.
var res=num1+num2; // result is Runs784 as java concats
two strings. Since JavaScript uses 32 bits signed integers,
it will not return 10. It will return -6.
Multiplicative Operators: Just like Additive operators,
multiplication(*), division(/) and modulo(%) are multiplicative 00000000000000000000000000000101 (5)
Operators. Modulo operator return remainder of division. 11111111111111111111111111111010 (~5 = -6)
Example 9 A signed integer uses the leftmost bit as the
minus sign.
javascript:alert(4%3); // returns 1
Additive and Multiplicative operator together can be called Equality operator:
Arithmetic Operator.
Equality operator are used to test whether two expressions
Bitwise operator: are the same.
JavaScript Uses 32 bits Bitwise Operands. JavaScript
stores numbers as 64 bits floating point numbers, but all For example "42" and 42 are equal with == but not equal
bitwise operations are performed on 32 bits binary with === as it not only checks for value but also check for
numbers.Before a bitwise operation is performed, type.
JavaScript converts numbers to 32 bits signed integers.
Relational Operator
After the bitwise operation is performed, the result is
converted back to 64 bits JavaScript numbers.
Relational operator checks whether one value is greater
Operator Name Description or less than other value.

& AND Sets each bit to 1 if both Operator Function


bits are 1
| OR Sets each bit to 1 if one of > Greater than
two bits is 1
< Less than
^ XOR Sets each bit to 1 if only
one of two bits is 1 >= Greater than or equal to
~ NOT Inverts all the bits
<= Less than or equal to
<< Zero fill Shifts left by pushing
left shift zeros in from the right and in Tests whether a value is found in
let the leftmost bits fall off an expression
>> Signed Shifts right by pushing
instanceof Tests whether an expression is an
right shift copies of the leftmost bit
instance of an object
in from the left, and let the
rightmost bits fall off
if(5<2) {
>>> Zero fill Shifts right by pushing
right shift zeros in from the left, and // do something
let the rightmost bits fall
off }

IT & ITES : COPA (NSQF Level - 4) - Related Theory for Exercise 2.1.96B 11

Copyright Free under CC BY Licence


The less than operator checks whether the first value is and q=a++; is equivalent to
less than second value and if valid, it returns false. In the
above example, 5 is not less than 2, so it is not true and q=a;
code inside the if block will not execute. a=a+1;
The other three operator are in the same way do checking
The delete Operator
for greater then (>),Greater than or equal to(>=), Less than
or equal to (<=). The delete operator can be used to delete properties from
objects.
in operator checks whether a given index is contained
within an object. For example: Example 13
var MyObj= {star:"Algol", constellation: "Perseus"}; var person = {firstName:"John", lastName:"Doe", age:50,
eyeColor:"blue"};
if("star" in MyObj) {
delete person.age;
// do something
The delete operator is designed to be used on object
}
properties. It has no effect on variables or functions.
As star is a index the code will work. but in operator do The delete operator should not be used on predefined Java
not work on numeric types as it works for numbers only. Script object properties. It can crash your application.
Instanceof Operator checks whether an object instance
The Unary + Operator
or object variable of is an instance of a particular object.
The unary + operator can be used to convert a variable to
Example 11
a number.
var mydate=new Date(); Example 14
if(mydate instanceof Date) {
var y = "5"; // y is a string
//do something var x = + y; // x is a number
}
If the variable cannot be converted, it will still become a
number, but with the value NaN (Not a number):
Here mydate is an instance of built-in Date object. So the
code will be executed within the if block. Example 15
Unary Operator
var y = "John"; // y is a string
delete, void, typeof, ++, --, + , -, ~ , ! are unary operators
var x = + y; // x is a number (NaN)
in Javascript.
In the same way Unary - Operator also operates.
Example 12
Ternary or Conditional Operator: It can be used as
a = -10;
compact if else.
p=++a; Example 16
q=a++;
a = (b>5 ? 4:7); means
s=+p;
if(b>5)
There are pre and post increment and decrement operator.
a=4;
p=++a; is equivalent to
else
a=a+1; a=7;
p=a;

12 IT & ITES : COPA (NSQF Level - 4) - Related Theory for Exercise 2.1.96B

Copyright Free under CC BY Licence


Assignment Operator: a = q; means value of q is assigned into a variable deleting
the previous value of a.
Assignment Operator is used to assign values into a
variable. Apart from = there are compound assignment Now a* = 3; is equivalent to a = a*3; and like that all other
operators as follows- compound assignment operator behaves.

*= /= %=

+= -= <<=

>>= >>>= &=

^= |=

IT & ITES : COPA (NSQF Level - 4) - Related Theory for Exercise 2.1.96B 13

Copyright Free under CC BY Licence

You might also like