Data Types and Data Types and Variables Variables Doncho Minkov Doncho Minkov Telerik Software Telerik Software Academy Academy http://academy.telerik.com http://academy.telerik.com Technical Trainer Technical Trainer http://minkov.it http://minkov.it
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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)
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
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::
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 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
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
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
// 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:
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