DATA TYPES Java Script has many data types

  • Slides: 15
Download presentation
DATA TYPES

DATA TYPES

ü Java Script has many data types: Numbers, Strings, Objects, etc. E. g: var

ü Java Script has many data types: Numbers, Strings, Objects, etc. E. g: var length=16; //Number var name=“Johnson”; //String var x={firstname: ”John”, lastname: ”son”}; //Object ü A computer cannot safely solve expressions without data types. E. g: var x = 16 + “Volvo”; var x = 16 + 4 + “Volvo”; var x = “Volvo” + 16 + 4;

ü Java Script data types are dynamic data types i. e same variable can

ü Java Script data types are dynamic data types i. e same variable can be used to hold different data types. E. g: var x; //Now x is undefined var x=5; //Now x is a number var x=“John”; //Now x is a string

ü The different data types in Java script are: (i) String (ii) Number (iii)

ü The different data types in Java script are: (i) String (ii) Number (iii) Boolean (iv) Object (v) Undefined (vi) Function

(i) String: üA string (or a text string) is a series of characters. üStrings

(i) String: üA string (or a text string) is a series of characters. üStrings are written with quotes. You can use single or double quotes: E. g: var x = "John"; // Using double quotes var x = ’John’; // Usingle quotes

ü You can use quotes inside a string, as long as they don't match

ü You can use quotes inside a string, as long as they don't match the quotes surrounding the string: E. g: var answer = "It's alright"; // Single quote inside double quotes var answer = "He is called 'Johnny' "; // Single quotes inside double quotes var answer = 'He is called "Johnny" '; // Double quotes inside single quotes var s= “John” son”; //error *********************************** E. g: var x = “John”; typeof x; //Returns String ***********************************

üAn empty value has nothing to do with undefined. üAn empty string has both

üAn empty value has nothing to do with undefined. üAn empty string has both a legal value and a type. E. g: var car = ""; // The value is "", the typeof is "string"

(ii) Number: üJava. Script has only one type of numbers. üNumbers can be written

(ii) Number: üJava. Script has only one type of numbers. üNumbers can be written with, or without decimals: E. g: var x 1 = 34. 00; // Written with decimals var x 2 = 34; // Written without decimals üExtra large or extra small numbers can be written with scientific (exponential) notation: E. g: var y = 123 e 5; // 12300000 var z = 123 e-5; // 0. 00123

(iii) Boolean: üBooleans can only have two values: true or false. *********************** E. g:

(iii) Boolean: üBooleans can only have two values: true or false. *********************** E. g: var x = true; typeof x; //Returns Boolean ***********************

(iv) Object: üIn Java. Script, arrays are treated as Objects. E. g: var names

(iv) Object: üIn Java. Script, arrays are treated as Objects. E. g: var names = [“john", “ravi", “sai"]; typeof names; //Returns Object üJava. Script objects are written with curly braces. üObject properties are written as name : value pairs, separated by commas. E. g: var person = {first. Name : "John", last. Name : "Doe", age : 50, eye. Color : "blue"}; typeof person; //returns Object

üIn Java. Script null is "nothing". It is supposed to be something that doesn't

üIn Java. Script null is "nothing". It is supposed to be something that doesn't exist. üUnfortunately, in Java. Script, the data type of null is an object. E. g 1: var person = {first. Name: "John", last. Name: "Doe", age: 50, eye. Color: "blue"}; person = null; // Now value is null, but type is still an object E. g 2: var a=null; //value is null typeof a; // data type is Object

(v) Undefined: üIn Java. Script, a variable without a value, has the value undefined.

(v) Undefined: üIn Java. Script, a variable without a value, has the value undefined. E. g: var a; // Value is undefined, type is undefined üAny variable can be emptied, by setting the value to undefined. The type will also be undefined. E. g var a=10; a = undefined; // Value is undefined, type is undefined

üYou can also empty an object by setting it to undefined: E. g: var

üYou can also empty an object by setting it to undefined: E. g: var person = {first. Name: "John", last. Name: "Doe", age: 50, eye. Color: "blue"}; person = undefined; // Now both value and type is undefined Difference Between Undefined and Null üUndefined and null are equal in value but different in type: E. g: typeof undefined // undefined typeof null // object

(vi) Function: typeof function my. Func(){} // Returns "function"

(vi) Function: typeof function my. Func(){} // Returns "function"