JavaScript (Part 2)- Primitive Datatypes, Variables, Constants, And Assignments
JavaScript (Part 2)- Primitive Datatypes, Variables, Constants, And Assignments
Ullrich Hustadt
2 Storing Values
Variables
Assignments
Constants
4 Further Reading
Booleans
• JavaScript has a boolean datatype
with constants true and false (case sensitive)
• JavaScript offers the following boolean operators
&& (conjunction) || (disjunction) ! (negation)
• The truth tables for the boolean operators are as follows:
A B (A && B) A B (A || B)
true true B (true) true true A (true)
true false B (false) true false A (true)
false true A (false) false true B (true)
false false A (false) false false B (false)
A (! A)
true false
false true
Boolean Operators
• As in almost every programming language the operators
&& (conjunction) || (disjunction) ! (negation)
are so-called short-circuit boolean operators:
A boolean expression is evaluated (using inorder traversal) only
as far as is necessary to determine the overall truth value of the
expression
Beware of Rounding
• Rounding is an arithmetic operation commonly included in
programming languages, but with different implementations:
• You should also check what values are returned or what errors are
caused by log(0), sqrt(-1), 1/0, 0/0
(we’ll do that in another lecture)
Strings
• In JavaScript a string literal is a sequence of characters surrounded by
single-quotes or double-quotes
" This is a string " " true "
’ This is also a string ’ ’519 ’
• The escape character \ can be used to include single quotes in
single-quoted strings and double quotes in double-quoted strings:
’ This isn \ ’ t a " number " ’
" ’ We won ’ t sing \" God Save the Queen .\" ’"
• The escape character \ also must be used to include \ in a string
" This is a backslash \\" ’ This is a backslash \\ ’
• Additional escape characters are available, but do not make much sense
in the context of HTML
\b (backspace) \f (form feed) \n (newline)
\r (carriage return) \t (tab)
COMP519 Web Programming Lecture 11 Slide L11 – 8
Primitive Data Types Strings
Strings
• JavaScript uses + for string concatenation
"519" + ’123 ’ // returns "519123"
’the ’ + ’end ’ // returns " theend "
String Operators
• There are a range of additional string operators, for example:
string.substr(start, [length])
Returns (up to) length characters of string beginning at start
"university".substr(3,2) // returns "ve" (count starts at 0)
string.indexOf(str, [start])
Returns the index number at which str starts in string after start
"university".indexOf("i",3) // returns 7 (count starts at 0)
string.match(regexp)
Returns an array of matching substrings for the regular expression
regexp in string
"0ab1".match(/[^0-9]/) // returns ["a"]
string.replace(regexp, str)
Replaces occurrences of regexp in string by str
"0ab1".replace(/[^0-9]/g,"c") // returns "0cc1"
Variables
• JavaScript allows values to be stored in variables
• A JavaScript identifier may consist of
letters, digits, the $ symbol, and underscore,
but cannot start with a digit
• JavaScript variable names are JavaScript identifiers
• JavaScript variable names are case sensitive
Variable Declarations
• Variables can be declared (within an execution context) using one of the
following statements:
var variable1 , variable2 , ...
var variable1 = value1 , variable2 = value2 , ...
• The second statement also initialises the variables
• A declaration does not specify the type of a variable
• A variable can be initialised without an explicit declaration
by assigning a value to it:
variable = value
• It is good practice to always declare variables
Variable Declarations
• In JavaScript, the use of the value of a variable that is neither declared
nor initialised will result in a reference error and execution of the
program stops
• A declared but uninitialised variable has the default value undefined
and has type undefined
var myVar1
var myVar2 = 5
console . log ( ’ myVar2 = ’, myVar2 )
console . log ( ’ myVar1 = ’, myVar1 )
console . log ( ’ myVar3 = ’, myVar3 )
myVar1 = undefined
myVar2 = 5
ex . js :5
console . log ( ’ myVar3
= ’, myVar3 );
^
ReferenceError : myVar3 is not defined
Variable Declarations
• All variable declarations within an execution context are processed first
before any other code
; this does not include their initialisation
console . log ( ’ myVar4 = ’ , myVar4 )
console . log ( ’ myVar5 = ’ , myVar5 )
console . log ( ’ myVar6 = ’ , myVar6 )
var myVar4
var myVar5 = 2
myVar4 = undefined
myVar5 = undefined
ex . js :3
console . log ( ’ myVar6
= ’, myVar6 );
^
ReferenceError : myVar6 is not defined
Variable Declarations
• All variable declarations within an execution context are processed first
before any other code
; this does not include their initialisation
• The same is not true for variables that are only ever initalised but not
declared
console . log ( ’ myVar7 = ’ , myVar7 )
myVar7 = 7
console . log ( ’ myVar7 = ’, myVar7 );
^
ReferenceError : myVar7 is not defined
Variable Declarations
• It is legal (though not sensible) to declare the same variable twice in the
same context
var myVar8 = 8
console . log ( ’ myVar8 = ’ , myVar8 )
var myVar8 = ’ eight ’
console . log ( ’ myVar8 = ’ , myVar8 )
myVar8 = 8
myVar8 = ’ eight ’
• Re-declaring a variable does not affect its value
var myVar9 = 9
console . log ( ’ myVar9 = ’ , myVar9 )
var myVar9
console . log ( ’ myVar9 = ’ , myVar9 )
myVar9 = 9
myVar9 = 9
Assignments
• JavaScript uses the equality sign = for assignments
student_id = 200846369;
As in Java, this is an assignment expression
• The value of an assignment expression is the value assigned
b = ( a = 0) + 1; // a has value 0 , b has value 1
Constants
• Some JavaScript dialects allow the definition of constants using
const variable1 = value1 , variable2 = value2 , ...
• defines one or more constants
• constants follow the same scope rules as variables