Docsity

Variable Scoping Rules - E-Commerce - Lecture Slides, Slides of Fundamentals of E-Commerce

Toggle sidebar
bg1
1
JAVASCRIPT SYNTAX
BASICS, PART 1
Scripting rules of thumb
JavaScript is case sensitive.
Identifiers as traditional, except can be unicode and can
beg
in wi
t
h
a
$.
beg t a $.
Avoid $ as it is commonly used in external frameworks
(JQuery)
Camel casing is traditional.
salesTaxRate, firstName
Beware that existing JavaScript components
()
inconsistent
(
p. xxi
)
.
Comments:
// for single line
/* */ for multi-line
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Variable Scoping Rules - E-Commerce - Lecture Slides and more Slides Fundamentals of E-Commerce in PDF only on Docsity!

1

JAVAS CRIPT SYNTAX

BASICS, PART 1

Scripting rules of thumb

JavaScript is case sensitive.

Identifiers as traditional, except can be unicode and can

begin with a $.beg t a $.

Avoid $ as it is commonly used in external frameworks (JQuery)

Camel casing is traditional.

salesTaxRate, firstName

Beware that existing JavaScript components

inconsistent (p. xxi).( )

Comments:

// for single line /* */ for multi-line

2

Scripting rules of thumb

Statements should be terminated with a semicolon,

even though parser will automatically put in if

omitted. Why?y

Postel's Law

Code blocks use { }. Conventional in JavaScript to use

even around single-line blocks (if statements) and to

use Java-style formatting.

if (condition) { ddo this; hi } Opening (or closing) brace goes on line with the statement that controls access to the code block.

Variables and typing

Variables are not explicitly typed, and type may be

changed implicitly through assignment.

''var' placed in front of identifier to denote a variable ' l d i f t f id tifi t d t i bl

declaration.

var name;

var age = 17;

var gender, grade;

var shoeSize = 7, hairColor = "red";

4

Type conversion functions

Boolean(val)—evaluates val and returns true or false.

true: boolean true, nonempty string, nonzero number, any object f lfalse: boolean false, empty string, 0, NaN, null, b l f l t t i 0 N N ll undefined.

Number(val)—performs numeric conversion on val

boolean true returns 1, false returns 0. if val is numeric, returns val. if val is undefined, returns NaN. null string returns 0.ll t i t 0 String containing a number ('123'), returns that number. String containing text, returns NaN.

Type conversion functions

parseInt(val)—returns val converted to an "integer"

If first non-whitespace character in val is not numeric or string is null, returns NaN. CContinues parsing val until reaches end or a non-numeric ti i l til h d i value. Returns that outcome. If numeric substring begins 0x, interpreted as hexadecimal. If begins with 0, interpreted as octal. Favored over Number since returns NaN on a null string val instead of 0. An optional second parameter can specify the base ofAn optional second parameter can specify the base of val. Returned value is always base 10. http://einstein.etsu.edu/~pittares/CSCI3110/examples/2-3.htm

5

Type conversion functions

parseFloat(val)—returns val converted to a "float"

Similar to parseInt, except

. is a valid part of a number, but can only occur once. Zeros at the beginning are ignoredZeros at the beginning are ignored. If val in hexadecimal form ("0x…") 0 is returned. If val in scientific notation form, float equivalent is returned. (2.7845e10)

val.toString() returns val as a string.

If val is null or undefined, "null" or "undefined" returned. If val is number, optional parameter can specify base ofIf val is number, optional parameter can specify base of output. var x = 10; var y = x.toString(2);

In class practice