ITSE 1411 Beginning Web
Data Types JS Module 2 Discussion
Discussion
- JavaScript Data Types
- Variables are assigned values. There are five "primitive" types of values in JavaScript that the variables represent.
A primitive type holds only one value.
- Number - numbers are of two different types:
- Integers - whole numbers with no decimal points.
- Float numbers - numbers with a decimal point.
- Boolean - a logical value that is either true or false.
- String - text.
- Undefined - a variable that has been declared but has not been defined (assigned a value).
- Null - an empty value. (In JavaScript you actually assign the reserved word "null".) For example:
var myName = "null";
- JavaScript is loosely typed meaning that a variable can be assigned different types.
For example you can declare a variable and assign it different types of data:
var myVariable = "Hello World";
myVariable = 6
- Numeric data types
- An integer is a number that does not have a decimal point. It can be either
positive or negative.
- A floating-point number is a number that has a decimal point. It can be either
positive or negative. When a floating-point is a very large number (or has a large
number of decimal places), the floating-point number is represented in scientific
notation. The number 200,000,000,000.0 is the same as the number 2.0e11
- Boolean values
- A Boolean value is a value with the value of true or false. Do not use quotes around
the word true or false. Some programming languages use 0 for false and 1 for true.
(A handy way to remember that 0 is false is 0 come before 1, and alphabetically
'f' comes before 't'.)
- Working with strings
- A string contains 0 or more characters.
- A string is contained in either a single or double quotation (but they must match).
- A zero length string is called an empty string. To assign a string variable the value
of an empty string the syntax is:
var someVariable = "";
- An empty string is not the same as a null string (which has the value null) or
an undefined string (a string that has not been assigned a value).
- Since quotes (either single or double) are used to contain a literal string, to
display either single or double quotes as part of the string takes some effort.
- Use single quotes to encase double quotes.
- Use double quotes to encase single quotes.
- The '+' operator is used in a string for concatenation (combining two strings).
You can also use the '+=' operator which will be explained in the expression
discussion with compound operators.
- Escape characters and sequences
- The escape character in JavaScript is the backslash '\'
- Quotations work in pairs to open and close strings. To use a
single quotation (for example writing a possessive word or a contraction)
use an escape character to show the following letter has a special
purpose. The escape character and an apostrophe example is
can\'t
which displays
can't