02. Data Type and Variables
-
Upload
donvercety -
Category
Education
-
view
296 -
download
0
Transcript of 02. Data Type and Variables
Data Types and Data Types and VariablesVariables
Doncho MinkovDoncho Minkov
Telerik Software Telerik Software AcademyAcademyhttp://academy.telerik.com http://academy.telerik.com
Technical TrainerTechnical Trainerhttp://minkov.it http://minkov.it
Table of ContentsTable of Contents
1.1. Data TypesData Types Integer Integer Floating-Point Floating-Point BooleanBoolean StringString
2.2. Declaring and Using VariablesDeclaring and Using Variables IdentifiersIdentifiers Declaring Variables and Assigning Declaring Variables and Assigning
ValuesValues
2
Data TypesData Types
How Computing Works?How Computing Works?
Computers are machines that Computers are machines that process dataprocess data Data is stored in the computer Data is stored in the computer
memory in memory in variablesvariables
Variables have Variables have namename, , data type data type and and valuevalue
Example of variable definition and Example of variable definition and assignment in JavaScriptassignment in JavaScript
4
var count = 5;var count = 5;
Variable nameVariable name
Variable Variable valuevalue
What Is a Data Type?What Is a Data Type? A A data typedata type::
Is a domain of values of similar Is a domain of values of similar characteristicscharacteristics
Defines the type of information Defines the type of information stored in the computer memory (in stored in the computer memory (in a variable)a variable)
Examples:Examples:
Positive integers: Positive integers: 11, , 22, , 33, , ……
Alphabetical characters: Alphabetical characters: aa, , bb, , cc, , ……5
JavaScript Data TypesJavaScript Data Types JavaScript is actually typeless JavaScript is actually typeless
languagelanguage i.e. the type of a variable can be i.e. the type of a variable can be
changedchanged
All the variables are declared with All the variables are declared with varvar
6
var count = 5; //variable holding integer valuevar count = 5; //variable holding integer valuevar name = "Doncho Minkov"; //variable holding a var name = "Doncho Minkov"; //variable holding a stringstringvar mark = 5.25 //variable holding floating point var mark = 5.25 //variable holding floating point numbernumber
Integer TypesInteger Types
What are Integer What are Integer Types?Types?
Integer types:Integer types: Represent whole numbersRepresent whole numbers
Have range of values, depending on Have range of values, depending on the size of memory usedthe size of memory used
8
Integer Types - ExampleInteger Types - Example
Integer type can hold numbers Integer type can hold numbers from from --90071992547409929007199254740992 toto 90071992547409929007199254740992
9
var studentsCount = 5;var studentsCount = 5;
var maxInteger = 9007199254740992;var maxInteger = 9007199254740992;
var minInteger = -9007199254740992;var minInteger = -9007199254740992;
Integer TypesInteger TypesLive DemoLive Demo
Floating-PointFloating-Point
What are Floating-Point What are Floating-Point Types?Types?
Floating-point types:Floating-point types:
Represent real numbersRepresent real numbers
Have range of values and precisionHave range of values and precision
Can behave abnormally in the Can behave abnormally in the calculationscalculations
12
Floating-Point TypesFloating-Point Types
Floating-point size depend on the Floating-point size depend on the platformplatform the browser and the OSthe browser and the OS
32-bit OS and browser have 32 bits 32-bit OS and browser have 32 bits for number, while 64-bit have 64 bitsfor number, while 64-bit have 64 bits It is good idea to use up to 32-bit It is good idea to use up to 32-bit
numbersnumbers Will always work on all platformsWill always work on all platforms
13
Abnormalities in the Abnormalities in the Floating-Point Floating-Point
CalculationsCalculations Sometimes abnormalities can be Sometimes abnormalities can be
observed when using floating-point observed when using floating-point numbersnumbers Comparing floating-point numbers Comparing floating-point numbers
can not be performed directly with can not be performed directly with the the ==== operator operator
Example:Example:
14
var a = 1.0;var a = 1.0;var b = 0.33;var b = 0.33;var sum = 1.33;var sum = 1.33;var equal = (a+b == sum); // False!!!var equal = (a+b == sum); // False!!!console.log("a+b = "+ (a+b) + ", sum = "+ console.log("a+b = "+ (a+b) + ", sum = "+ sum + ", sum == a+b? is " + equal);sum + ", sum == a+b? is " + equal);
Floating-Point and Floating-Point and Decimal Floating-Decimal Floating-
Point TypesPoint TypesLive DemoLive Demo
Boolean TypeBoolean Type
The Boolean Data TypeThe Boolean Data Type
The The Boolean data typeBoolean data type:: Has two possible values: Has two possible values:
truetrue and and falsefalse
Is useful in logical expressionsIs useful in logical expressions
17
Boolean Values – Boolean Values – ExampleExample
Example of boolean variables Example of boolean variables taking values of taking values of truetrue or or falsefalse::
18
var a = 1;var a = 1;var b = 2;var b = 2;
var greaterAB = (a > b);var greaterAB = (a > b);
console.log(greaterAB); // Falseconsole.log(greaterAB); // False
var equalA1 = (a == 1);var equalA1 = (a == 1);
console.log(equalA1); // Trueconsole.log(equalA1); // True
Boolean TypeBoolean TypeLive DemoLive Demo
String TypeString Type
The String Data TypeThe String Data Type The The string data typestring data type::
Represents a sequence of Represents a sequence of characterscharacters
Strings are enclosed in quotes:Strings are enclosed in quotes: Both Both '' and and "" work work
Strings can be concatenatedStrings can be concatenated Using the Using the ++ operator operator
21
var s = "Microsoft .NET Framework";var s = "Microsoft .NET Framework";
Saying Hello – ExampleSaying Hello – Example Concatenating the two names of a Concatenating the two names of a
person to obtain his full name:person to obtain his full name:
NOTE: a space is missing between NOTE: a space is missing between the two names! We have to add it the two names! We have to add it manuallymanually 22
var firstname = "Ivan";var firstname = "Ivan";var lastname = "Ivanov";var lastname = "Ivanov";Console.log("Hello, " + firstname + "!");Console.log("Hello, " + firstname + "!");
var fullName = firstname + " " + lastname;var fullName = firstname + " " + lastname;Console.log("Your full name is " + Console.log("Your full name is " + fullname);fullname);
String TypeString TypeLive DemoLive Demo
Introducing Introducing VariablesVariables
pp qq
ii
What Is a Variable?What Is a Variable? A variable is a:A variable is a:
Placeholder of information that can Placeholder of information that can usually be changed at run-timeusually be changed at run-time
Variables allow you to:Variables allow you to: Store informationStore information
Retrieve the stored informationRetrieve the stored information
Manipulate the stored informationManipulate the stored information
25
Variable CharacteristicsVariable Characteristics A variable has:A variable has:
NameName Type (of stored data)Type (of stored data) ValueValue
Example:Example:
Name: Name: countercounter Type: Type: integerinteger Value: Value: 55
26
var counter = 5;var counter = 5;
Declaring And Declaring And Using VariablesUsing Variables
Declaring VariablesDeclaring Variables When declaring a variable we:When declaring a variable we:
Specify its name (called identifier)Specify its name (called identifier)
May give it an initial valueMay give it an initial value
The type is identified by the valueThe type is identified by the value
The syntax is the following:The syntax is the following:
Example:Example:
28
var <identifier> [= <initialization>];var <identifier> [= <initialization>];
var height = 200;var height = 200;
IdentifiersIdentifiers Identifiers may consist of:Identifiers may consist of:
Letters (Unicode) Letters (Unicode)
Digits [0-9]Digits [0-9]
Underscore "_"Underscore "_"
Dollar '$'Dollar '$'
IdentifiersIdentifiers Can begin only with a letter or an Can begin only with a letter or an
underscoreunderscore
Cannot be a JavaScript keywordCannot be a JavaScript keyword29
Identifiers (2)Identifiers (2) IdentifiersIdentifiers
Should have a descriptive nameShould have a descriptive name
It is recommended to use only Latin It is recommended to use only Latin lettersletters
Should be neither too long nor too Should be neither too long nor too shortshort
Note:Note: In JavaScript small letters are In JavaScript small letters are
considered different than the considered different than the capital letters (case sensitivity)capital letters (case sensitivity)
30
Identifiers – ExamplesIdentifiers – Examples Examples of correct identifiers:Examples of correct identifiers:
Examples of incorrect identifiers:Examples of incorrect identifiers:
31
var new;var new; // new is a keyword// new is a keywordvar 2Pac;var 2Pac; // Cannot begin with a digit// Cannot begin with a digit
var New = 2; // Here N is capitalvar New = 2; // Here N is capitalvar _2Pac; // This identifiers begins with _var _2Pac; // This identifiers begins with _
var поздрав = "Hello"; // Unicode symbols usedvar поздрав = "Hello"; // Unicode symbols used// The following is more appropriate:// The following is more appropriate:var greeting = "Hello"; var greeting = "Hello";
var n = 100; // Undescriptivevar n = 100; // Undescriptivevar numberOfClients = 100; // Descriptivevar numberOfClients = 100; // Descriptive
// Overdescriptive identifier:// Overdescriptive identifier:var numberOfPrivateClientOfTheFirm = 100;var numberOfPrivateClientOfTheFirm = 100;
Assigning Assigning Values To Values To VariablesVariables
Assigning ValuesAssigning Values Assigning of values to variablesAssigning of values to variables
Is achieved by the Is achieved by the == operator operator
The The == operator has operator has Variable identifier on the leftVariable identifier on the left
Value of the corresponding data Value of the corresponding data type on the righttype on the right
Could be used in a cascade calling, Could be used in a cascade calling, where assigning is done from right where assigning is done from right to leftto left
33
Assigning Values – Assigning Values – ExamplesExamples
Assigning values example:Assigning values example:
34
var firstValue = 5;var firstValue = 5;var secondValue;var secondValue;var thirdValue;var thirdValue;
// Using an already declared variable:// Using an already declared variable:secondValue = firstValue;secondValue = firstValue;
// The following cascade calling assigns// The following cascade calling assigns// 3 to firstValue and then firstValue// 3 to firstValue and then firstValue// to thirdValue, so both variables have// to thirdValue, so both variables have// the value 3 as a result:// the value 3 as a result:
thirdValue = firstValue = 3; // Avoid this!thirdValue = firstValue = 3; // Avoid this!
Initializing VariablesInitializing Variables
InitializingInitializing
Is assigning of initial valueIs assigning of initial value
Must be done before the variable is Must be done before the variable is used!used!
Several ways of initializing:Several ways of initializing:
By using a literal expressionBy using a literal expression
By referring to an already initialized By referring to an already initialized variablevariable
35
Initialization – Initialization – ExamplesExamples
Example of some initializations:Example of some initializations:
36
// This is how we use a literal expression:// This is how we use a literal expression:var heightInMeters = 1.74;var heightInMeters = 1.74;
// Here we use an already initialized // Here we use an already initialized variable:variable:var greeting = "Hello World!";var greeting = "Hello World!";var message = greeting;var message = greeting;
Assigning and Assigning and Initializing Initializing VariablesVariables
Live DemoLive Demo
Data Types and Data Types and VariablesVariables
http://http://academy.telerik.comacademy.telerik.com