SlideShare a Scribd company logo
COURSE 08
JAVASCRIPT
lect. eng. Rajmond JÁNÓ, PhD
rajmond.jano@ael.utcl
uj.ro
fb.com/janorajmond
C08 – JAVASCRIPT
• Conditional statements
• Loops
• Events
• Strict mode
• Error handling
• Debugging
• Style guides & coding conventions
• Best practices
• Common mistakes
• Performance tips
CONDITIONAL STATEMENTS
In JavaScript we have the following conditional statements:
• Use if to specify a block of code to be executed, if a
specified condition is true
• Use else to specify a block of code to be executed, if
the same condition is false
• Use else if to specify a new condition to test, if the
first condition is false
• Use switch to specify many alternative blocks of code
to be executed
CONDITIONAL STATEMENTS
CONDITIONAL STATEMENTS
CONDITIONAL STATEMENTS
CONDITIONAL STATEMENTS
LOOPS
JavaScript has the following loop instructions:
• while - loops through a block of code while a
specified condition is true
• do/while - also loops through a block of code
while a specified condition is true
• for - loops through a block of code a number of
times
• for/in - loops through the properties of an
object
• for/of - loops through the values of an iterable
object
LOOPS
LOOPS
LOOPS
Executed once before
the execution of the
code block
Condition for executing
the code block
Executed every time
after the execution of
the code block
LOOPS
• For/in statements loop through the
properties of an object
LOOPS
• For/of statements loop through the values
of an iterable objects
EVENTS
• HTML events are "things" that happen to HTML
elements
• When JavaScript is used in HTML pages, JavaScript
can "react" to these events
• An HTML event can be something the browser
does, or something a user does
• Here are some examples of HTML events:
• An HTML web page has finished loading
• An HTML input field was changed
• An HTML button was clicked
EVENTS
• Often, when events happen, you may want to
do something
• JavaScript lets you execute code when events
are detected
• HTML allows event handler attributes, with
JavaScript code, to be added to HTML elements
EVENTS
EVENTS
Event Description
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover
The user moves the mouse over an HTML
element
onmouseout
The user moves the mouse away from an
HTML element
onkeydown The user pushes a keyboard key
onload
The browser has finished loading the
page
and many more…
EVENTS
Event handlers can be used to handle, and
verify, user input, user actions, and browser
actions:
• Things that should be done every time a page loads
• Things that should be done when the page is closed
• Action that should be performed when a user clicks
a button
• Content that should be verified when a user inputs
data
• And more ..
EVENTS
Many different methods can be used to let
JavaScript work with events:
• HTML event attributes can execute JavaScript code
directly
• HTML event attributes can call JavaScript functions
• You can assign your own event handler functions to
HTML elements
• You can prevent events from being sent or being
handled
• And more ...
DATE OBJECTS
Whenever you don’t have a date, and really
would like one, JavaScript can help: you can
create a new Date object
DATE OBJECTS
Method Description
getFullYear()
Get the year as a four digit
number (yyyy)
getMonth()
Get the month as a number (0-
11)
getDate() Get the day as a number (1-31)
getHours() Get the hour (0-23)
getMinutes() Get the minute (0-59)
getSeconds() Get the second (0-59)
getMilliseconds() Get the millisecond (0-999)
getTime()
Get the time
(milliseconds since January 1,
1970)
getDay()
Get the weekday as a number
(0-6)
DATE OBJECTS
Date object methods:
getMonth() and getDay() are 0 indexed!
STRICT MODE
• Strict mode makes it easier to write "secure"
JavaScript
• Strict mode changes previously accepted "bad
syntax" into real errors
• As an example, in normal JavaScript, mistyping a
variable name creates a new global variable. In strict
mode, this will throw an error, making it impossible to
accidentally create a global variable
• In normal JavaScript, a developer will not receive
any error feedback assigning values to non-
writable properties
• In strict mode, any assignment to a non-writable
property, a getter-only property, a non-existing
STRICT MODE
• Strict mode is declared by adding "use
strict"; to the beginning of a script or a
function
• Declared at the beginning of a script, it has
global scope
• Declared inside a function, it has local scope
STRICT MODE
ERROR HANDLING
• The try statement allows you to define a block
of code to be tested for errors while it is being
executed
• The catch statement allows you to define a
block of code to be executed, if an error
occurs in the try block
• The throw statement allows you to create a
custom error
• The finally statement lets you execute code,
ERROR HANDLING
DEBUGGING
• Programming code might contain syntax errors, or
logical errors
• Many of these errors are difficult to diagnose
• Often, when programming code contains errors,
nothing will happen. There are no error messages,
and you will get no indications where to search for
errors
• Searching for (and fixing) errors in programming
code is called code debugging
DEBUGGING
• Debugging is not easy. But fortunately, all
modern browsers have a built-in JavaScript
debugger
• Built-in debuggers can be turned on and off,
forcing errors to be reported to the user
• With a debugger, you can also set breakpoints
(places where code execution can be stopped),
and examine variables while the code is
executing
DEBUGGING
• Normally, you activate debugging in your
browser with F12, and selecting "Console" in
the debugger menu
• Otherwise follow these steps
Chrome • From the menu, select "More tools"
• From tools, choose "Developer tools"
• Finally, select Console.
Firefox • From the menu, select "Web Developer"
• Finally, select "Web Console"
Edge • From the menu, select "Developer Tools"
• Finally, select "Console"
Opera • From the menu, select "Developer"
• From "Developer", select "Developer tools"
• Finally, select "Console"
Safari • Go to Safari, Preferences, Advanced in the main menu
• Check "Enable Show Develop menu in menu bar"
• When the new option "Develop" appears in the menu: Choose "Show
Error Console"
DEBUGGING
• If your browser supports debugging, you can
use console.log() to display JavaScript
values in the debugger window
DEBUGGING
• If your browser supports debugging, you can
use console.log() to display JavaScript
values in the debugger window
DEBUGGING
• If your browser supports debugging, you can
use console.log() to display JavaScript
values in the debugger window
DEBUGGING
• In the debugger window, you can set breakpoints
in the JavaScript code
• At each breakpoint, JavaScript will stop executing,
and let you examine JavaScript values
• You can step over or step into functions and watch
variables
• After examining values, you can resume the
execution of code (typically with a play button)
DEBUGGING
DEBUGGING
• The debugger keyword stops the execution of
JavaScript, and calls (if available) the
debugging function
• This has the same function as setting a
breakpoint in the debugger
• If no debugging is available, the debugger
statement has no effect
DEBUGGING
STYLE GUIDES & CODING
CONVENTIONS
Coding conventions are style guidelines for
programming. They typically cover:
• Naming and declaration rules for variables and
functions
• Rules for the use of white space, indentation, and
comments
• Programming practices and principles
Coding conventions secure quality:
• Improve code readability
• Make code maintenance easier
STYLE GUIDES & CODING
CONVENTIONS
Code blocks
• The opening curly brackets { of the corresponding
code block start on the same line as the statement
they belong to but end on a new line
• Indent code to represent code blocks ownership
STYLE GUIDES & CODING
CONVENTIONS
Variable Names
• Use camelCase to name your variables
• All variable names start with a letter (except jQuery
DOM cache variables, which start with $ – will be
covered in C11)
• Remember that hyphens (-) are not allowed in
JavaScript
STYLE GUIDES & CODING
CONVENTIONS
• Always put spaces around operators and after
commas
STYLE GUIDES & CODING
CONVENTIONS
• General rules for simple statements:
• Always end a simple statement with a semicolon
• General rules for complex (compound)
statements:
• Put the opening bracket at the end of the first line
• Use one space before the opening bracket
• Put the closing bracket on a new line, without
leading spaces
• Do not end a complex statement with a semicolon
STYLE GUIDES & CODING
CONVENTIONS
General rules for object definitions:
• Place the opening bracket on the same line as the
object name
• Use colon plus one space between each property and
its value
• Use quotes around string values, not around
numeric values
• Do not add a comma after the last property-value
pair
• Place the closing bracket on a new line, without
leading spaces
STYLE GUIDES & CODING
CONVENTIONS
General rules for object definitions:
STYLE GUIDES & CODING
CONVENTIONS
• For readability, avoid lines longer than 80
characters
• If a JavaScript statement does not fit on one
line, the best place to break it, is after an
operator or a comma
STYLE GUIDES & CODING
CONVENTIONS
Loading JavaScript in HTML
• Use simple syntax for loading external scripts (the
type attribute is not necessary)
STYLE GUIDES & CODING
CONVENTIONS
• A consequence of using "untidy" HTML styles,
might result in JavaScript errors
• These two JavaScript statements will produce
different results:
• If possible, use the same naming convention (as
JavaScript) in HTML
STYLE GUIDES & CODING
CONVENTIONS
File extensions
• HTML files should have a .html extension (not .htm).
• CSS files should have a .css extension.
• JavaScript files should have a .js extension
File names
• Use Lower Case File Names
• Most web servers (Apache, Unix) are case sensitive about file
names (london.jpg cannot be accessed as London.jpg)
• Other web servers (Microsoft, IIS) are not case sensitive
(london.jpg can be accessed as London.jpg or london.jpg)
• If you use a mix of upper and lower case, you have to be
extremely consistent
• If you move from a case insensitive, to a case sensitive server,
even small errors can break your web site
• To avoid these problems, always use lower case file names (if
STYLE GUIDES & CODING
CONVENTIONS
Performance
• Coding conventions are not used by computers.
Most rules have little impact on the execution of
programs
• Indentation and extra spaces are not significant in
small scripts
• For code in development, readability should be
preferred. Larger production scripts should be
minified
BEST PRACTICES
Avoid Global Variables
• Minimize the use of global variables
• This includes all data types, objects, and functions
• Global variables and functions can be overwritten by
other scripts
• Use local variables instead
BEST PRACTICES
Always Declare Local Variables
• All variables used in a function should be declared
as local variables
• Local variables must be declared with the var
keyword or the let keyword, otherwise they will
become global variables
• Strict mode does not allow undeclared variables
BEST PRACTICES
Declarations on Top
• It is a good coding practice to put all declarations at
the top of each script or function
• This will:
• Give cleaner code
• Provide a single place to look for local variables
• Make it easier to avoid unwanted (implied) global
variables
• Reduce the possibility of unwanted re-declarations
BEST PRACTICES
Initialize Variables
• It is a good coding practice to initialize variables
when you declare them
• This will:
• Give cleaner code
• Provide a single place to initialize variables
• Avoid undefined values
BEST PRACTICES
Never Declare Number, String, or Boolean
Objects
• Always treat numbers, strings, or booleans as
primitive values, not as objects
• Declaring these types as objects, slows down
execution speed, and produces nasty side effects
BEST PRACTICES
Don't Use new Object()
• Use {} instead of new Object()
• Use "" instead of new String()
• Use 0 instead of new Number()
• Use false instead of new Boolean()
• Use [] instead of new Array()
• Use /()/ instead of new RegExp()
• Use function (){} instead of new Function()
BEST PRACTICES
Use === Comparison
• The == comparison operator always converts (to
matching types) before comparison
• The === operator forces comparison of values and
type
BEST PRACTICES
Use Parameter Defaults
• If a function is called with a missing argument, the
value of the missing argument is set to undefined
• Undefined values can break your code. It is a good
habit to assign default values to arguments
Default
parameter
values
BEST PRACTICES
End your switches with defaults
• Always end your switch statements with a default.
Even if you think there is no need for it
BEST PRACTICES
Use you IDE to automatically format your code
• “Smart” IDEs (Visual Studio) automatically format
code according to most common formatting
guidelines
• To manually force Visual Studio 201x to reformat
code use Ctrl+K, D
• To manually force Visual Studio Code to reformat
code use Alt+Shift+F
COMMON MISTAKES
Accidentally Using the Assignment Operator
• JavaScript programs may generate unexpected
results if a programmer accidentally uses an
assignment operator (=), instead of a comparison
operator (== or ===) in an if statement
COMMON MISTAKES
Expecting Loose Comparison
• In regular comparison, data type does not matter
• switch case expressions use strict comparison
COMMON MISTAKES
Misunderstanding Floats
• All numbers in JavaScript are stored as 64-bits
Floating point numbers (Floats).
• All programming languages, including
JavaScript, have difficulties with precise
floating point values
COMMON MISTAKES
Breaking a JavaScript String
• JavaScript will allow you to break a statement into
two lines but, breaking a statement in the middle of
a string will not work
• You must use a "backslash" if you must break a
statement in a string
COMMON MISTAKES
Misplacing semicolon
• Because of a misplaced semicolon, this code block
will execute regardless of the value of x
COMMON MISTAKES
• JavaScript tries to add automatic semicolons
where it considers them to be missing
• This can cause unexpected behavior if code
blocks are defined with line breaks
JS will insert an
automatic semicolon
PERFORMANCE TIPS
Reduce Activity in Loops
• Each statement in a loop, including the for
statement, is executed for each iteration of the loop
• Statements or assignments that can be placed
outside the loop will make the loop run faster
Bad Good
PERFORMANCE TIPS
Reduce DOM Access
• Accessing the HTML DOM is very slow, compared to
other JavaScript statements
• If you expect to access a DOM element several
times, access it once, and use it as a local variable
(called caching the DOM)
PERFORMANCE TIPS
Avoid Unnecessary Variables
• Don't create new variables if you don't plan to save
values
Bad
Good
PERFORMANCE TIPS
Delay JavaScript loading
• Putting your scripts at the bottom of the page
body lets the browser load the page first
• While a script is downloading, the browser will
not start any other downloads. In addition all
parsing and rendering activity might be
blocked
• The HTTP specification defines that browsers should
not download more than two components in parallel
PERFORMANCE TIPS
Delay JavaScript loading
• An alternative is to use defer in the script tag. The
defer attribute specifies that the script should be
executed after the page has finished parsing, but it
only works for external scripts.
• If possible, you can add your script to the page by
code, after the page has loaded
BROWSER SUPPORT
Version
Browser
Chrome Firefox IE Edge Safari Opera
ES5
(2009)
23 21 9 10 6 15
ECMAScri
pt 2015
51 54 N/A 14 10 38
ECMAScri
pt 2016
68 N/A N/A N/A N/A 55
BIBLIOGRAPHY
• https://www.w3schools.com/js/default.asp
• https://github.com/denysdovhan/wtfjs
COURSES
Available online at:
http://www.ael.utcluj.ro/
Information for Students -> Courses -> Web
Technologies
Web technologies-course 08.pptx

More Related Content

Similar to Web technologies-course 08.pptx

JavaScript - Chapter 15 - Debugging Techniques
 JavaScript - Chapter 15 - Debugging Techniques JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
Integrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMSIntegrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMS
Tom Borger
 
Intro to JavaScript - Thinkful LA, June 2017
Intro to JavaScript - Thinkful LA, June 2017Intro to JavaScript - Thinkful LA, June 2017
Intro to JavaScript - Thinkful LA, June 2017
Thinkful
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
VAIBHAVKADAGANCHI
 
Error handling and debugging in vb
Error handling and debugging in vbError handling and debugging in vb
Error handling and debugging in vb
Salim M
 
Hsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdfHsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdf
AAFREEN SHAIKH
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
Thinkful - Intro to JavaScript
Thinkful - Intro to JavaScriptThinkful - Intro to JavaScript
Thinkful - Intro to JavaScript
TJ Stalcup
 
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
Sam Bowne
 
Intro to javascript (5:2)
Intro to javascript (5:2)Intro to javascript (5:2)
Intro to javascript (5:2)
Thinkful
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
Java Script basics and DOM
Java Script basics and DOMJava Script basics and DOM
Java Script basics and DOM
Sukrit Gupta
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
sandeshshahapur
 
Unit - 4 all script are here Javascript.pptx
Unit - 4 all script are here Javascript.pptxUnit - 4 all script are here Javascript.pptx
Unit - 4 all script are here Javascript.pptx
kushwahanitesh592
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
ssusere336f4
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
Heartin Jacob
 
Exception handling
Exception handlingException handling
Exception handling
pooja kumari
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
Mukund Trivedi
 
How to crack java script certification
How to crack java script certificationHow to crack java script certification
How to crack java script certification
KadharBashaJ
 

Similar to Web technologies-course 08.pptx (20)

JavaScript - Chapter 15 - Debugging Techniques
 JavaScript - Chapter 15 - Debugging Techniques JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging Techniques
 
Integrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMSIntegrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMS
 
Intro to JavaScript - Thinkful LA, June 2017
Intro to JavaScript - Thinkful LA, June 2017Intro to JavaScript - Thinkful LA, June 2017
Intro to JavaScript - Thinkful LA, June 2017
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Error handling and debugging in vb
Error handling and debugging in vbError handling and debugging in vb
Error handling and debugging in vb
 
Hsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdfHsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdf
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Thinkful - Intro to JavaScript
Thinkful - Intro to JavaScriptThinkful - Intro to JavaScript
Thinkful - Intro to JavaScript
 
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
 
Intro to javascript (5:2)
Intro to javascript (5:2)Intro to javascript (5:2)
Intro to javascript (5:2)
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
Java Script basics and DOM
Java Script basics and DOMJava Script basics and DOM
Java Script basics and DOM
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
 
Unit - 4 all script are here Javascript.pptx
Unit - 4 all script are here Javascript.pptxUnit - 4 all script are here Javascript.pptx
Unit - 4 all script are here Javascript.pptx
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Exception handling
Exception handlingException handling
Exception handling
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
 
How to crack java script certification
How to crack java script certificationHow to crack java script certification
How to crack java script certification
 

More from Stefan Oprea

Training-Book-Samsung.ppt
Training-Book-Samsung.pptTraining-Book-Samsung.ppt
Training-Book-Samsung.ppt
Stefan Oprea
 
PRESENTATION ON TOUCH TECHNOLOGY.ppt
PRESENTATION ON TOUCH TECHNOLOGY.pptPRESENTATION ON TOUCH TECHNOLOGY.ppt
PRESENTATION ON TOUCH TECHNOLOGY.ppt
Stefan Oprea
 
Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
Stefan Oprea
 
Web technologies-course 11.pptx
Web technologies-course 11.pptxWeb technologies-course 11.pptx
Web technologies-course 11.pptx
Stefan Oprea
 
Web technologies-course 10.pptx
Web technologies-course 10.pptxWeb technologies-course 10.pptx
Web technologies-course 10.pptx
Stefan Oprea
 
Web technologies-course 09.pptx
Web technologies-course 09.pptxWeb technologies-course 09.pptx
Web technologies-course 09.pptx
Stefan Oprea
 
Web technologies-course 07.pptx
Web technologies-course 07.pptxWeb technologies-course 07.pptx
Web technologies-course 07.pptx
Stefan Oprea
 
Web technologies-course 06.pptx
Web technologies-course 06.pptxWeb technologies-course 06.pptx
Web technologies-course 06.pptx
Stefan Oprea
 
Web technologies-course 05.pptx
Web technologies-course 05.pptxWeb technologies-course 05.pptx
Web technologies-course 05.pptx
Stefan Oprea
 
Web technologies-course 04.pptx
Web technologies-course 04.pptxWeb technologies-course 04.pptx
Web technologies-course 04.pptx
Stefan Oprea
 
Web technologies-course 03.pptx
Web technologies-course 03.pptxWeb technologies-course 03.pptx
Web technologies-course 03.pptx
Stefan Oprea
 
Web technologies-course 02.pptx
Web technologies-course 02.pptxWeb technologies-course 02.pptx
Web technologies-course 02.pptx
Stefan Oprea
 
Web technologies-course 01.pptx
Web technologies-course 01.pptxWeb technologies-course 01.pptx
Web technologies-course 01.pptx
Stefan Oprea
 
Fundamentals of Digital Modulation.ppt
Fundamentals of Digital Modulation.pptFundamentals of Digital Modulation.ppt
Fundamentals of Digital Modulation.ppt
Stefan Oprea
 
Orthogonal Frequency Division Multiplexing.ppt
Orthogonal Frequency Division Multiplexing.pptOrthogonal Frequency Division Multiplexing.ppt
Orthogonal Frequency Division Multiplexing.ppt
Stefan Oprea
 
Modulation tutorial.ppt
Modulation tutorial.pptModulation tutorial.ppt
Modulation tutorial.ppt
Stefan Oprea
 
Comparison of Single Carrier and Multi-carrier.ppt
Comparison of Single Carrier and Multi-carrier.pptComparison of Single Carrier and Multi-carrier.ppt
Comparison of Single Carrier and Multi-carrier.ppt
Stefan Oprea
 
OFDM and MC-CDMA An Implementation using MATLAB.ppt
OFDM and MC-CDMA An Implementation using MATLAB.pptOFDM and MC-CDMA An Implementation using MATLAB.ppt
OFDM and MC-CDMA An Implementation using MATLAB.ppt
Stefan Oprea
 
Concepts of 3GPP LTE.ppt
Concepts of 3GPP LTE.pptConcepts of 3GPP LTE.ppt
Concepts of 3GPP LTE.ppt
Stefan Oprea
 
Multi-Carrier Transmission over Mobile Radio Channels.ppt
Multi-Carrier Transmission over Mobile Radio Channels.pptMulti-Carrier Transmission over Mobile Radio Channels.ppt
Multi-Carrier Transmission over Mobile Radio Channels.ppt
Stefan Oprea
 

More from Stefan Oprea (20)

Training-Book-Samsung.ppt
Training-Book-Samsung.pptTraining-Book-Samsung.ppt
Training-Book-Samsung.ppt
 
PRESENTATION ON TOUCH TECHNOLOGY.ppt
PRESENTATION ON TOUCH TECHNOLOGY.pptPRESENTATION ON TOUCH TECHNOLOGY.ppt
PRESENTATION ON TOUCH TECHNOLOGY.ppt
 
Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
 
Web technologies-course 11.pptx
Web technologies-course 11.pptxWeb technologies-course 11.pptx
Web technologies-course 11.pptx
 
Web technologies-course 10.pptx
Web technologies-course 10.pptxWeb technologies-course 10.pptx
Web technologies-course 10.pptx
 
Web technologies-course 09.pptx
Web technologies-course 09.pptxWeb technologies-course 09.pptx
Web technologies-course 09.pptx
 
Web technologies-course 07.pptx
Web technologies-course 07.pptxWeb technologies-course 07.pptx
Web technologies-course 07.pptx
 
Web technologies-course 06.pptx
Web technologies-course 06.pptxWeb technologies-course 06.pptx
Web technologies-course 06.pptx
 
Web technologies-course 05.pptx
Web technologies-course 05.pptxWeb technologies-course 05.pptx
Web technologies-course 05.pptx
 
Web technologies-course 04.pptx
Web technologies-course 04.pptxWeb technologies-course 04.pptx
Web technologies-course 04.pptx
 
Web technologies-course 03.pptx
Web technologies-course 03.pptxWeb technologies-course 03.pptx
Web technologies-course 03.pptx
 
Web technologies-course 02.pptx
Web technologies-course 02.pptxWeb technologies-course 02.pptx
Web technologies-course 02.pptx
 
Web technologies-course 01.pptx
Web technologies-course 01.pptxWeb technologies-course 01.pptx
Web technologies-course 01.pptx
 
Fundamentals of Digital Modulation.ppt
Fundamentals of Digital Modulation.pptFundamentals of Digital Modulation.ppt
Fundamentals of Digital Modulation.ppt
 
Orthogonal Frequency Division Multiplexing.ppt
Orthogonal Frequency Division Multiplexing.pptOrthogonal Frequency Division Multiplexing.ppt
Orthogonal Frequency Division Multiplexing.ppt
 
Modulation tutorial.ppt
Modulation tutorial.pptModulation tutorial.ppt
Modulation tutorial.ppt
 
Comparison of Single Carrier and Multi-carrier.ppt
Comparison of Single Carrier and Multi-carrier.pptComparison of Single Carrier and Multi-carrier.ppt
Comparison of Single Carrier and Multi-carrier.ppt
 
OFDM and MC-CDMA An Implementation using MATLAB.ppt
OFDM and MC-CDMA An Implementation using MATLAB.pptOFDM and MC-CDMA An Implementation using MATLAB.ppt
OFDM and MC-CDMA An Implementation using MATLAB.ppt
 
Concepts of 3GPP LTE.ppt
Concepts of 3GPP LTE.pptConcepts of 3GPP LTE.ppt
Concepts of 3GPP LTE.ppt
 
Multi-Carrier Transmission over Mobile Radio Channels.ppt
Multi-Carrier Transmission over Mobile Radio Channels.pptMulti-Carrier Transmission over Mobile Radio Channels.ppt
Multi-Carrier Transmission over Mobile Radio Channels.ppt
 

Recently uploaded

The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 

Recently uploaded (20)

The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 

Web technologies-course 08.pptx

  • 1.
  • 2. COURSE 08 JAVASCRIPT lect. eng. Rajmond JÁNÓ, PhD rajmond.jano@ael.utcl uj.ro fb.com/janorajmond
  • 3.
  • 4. C08 – JAVASCRIPT • Conditional statements • Loops • Events • Strict mode • Error handling • Debugging • Style guides & coding conventions • Best practices • Common mistakes • Performance tips
  • 5. CONDITIONAL STATEMENTS In JavaScript we have the following conditional statements: • Use if to specify a block of code to be executed, if a specified condition is true • Use else to specify a block of code to be executed, if the same condition is false • Use else if to specify a new condition to test, if the first condition is false • Use switch to specify many alternative blocks of code to be executed
  • 10. LOOPS JavaScript has the following loop instructions: • while - loops through a block of code while a specified condition is true • do/while - also loops through a block of code while a specified condition is true • for - loops through a block of code a number of times • for/in - loops through the properties of an object • for/of - loops through the values of an iterable object
  • 11. LOOPS
  • 12. LOOPS
  • 13. LOOPS Executed once before the execution of the code block Condition for executing the code block Executed every time after the execution of the code block
  • 14. LOOPS • For/in statements loop through the properties of an object
  • 15. LOOPS • For/of statements loop through the values of an iterable objects
  • 16. EVENTS • HTML events are "things" that happen to HTML elements • When JavaScript is used in HTML pages, JavaScript can "react" to these events • An HTML event can be something the browser does, or something a user does • Here are some examples of HTML events: • An HTML web page has finished loading • An HTML input field was changed • An HTML button was clicked
  • 17. EVENTS • Often, when events happen, you may want to do something • JavaScript lets you execute code when events are detected • HTML allows event handler attributes, with JavaScript code, to be added to HTML elements
  • 19. EVENTS Event Description onchange An HTML element has been changed onclick The user clicks an HTML element onmouseover The user moves the mouse over an HTML element onmouseout The user moves the mouse away from an HTML element onkeydown The user pushes a keyboard key onload The browser has finished loading the page and many more…
  • 20. EVENTS Event handlers can be used to handle, and verify, user input, user actions, and browser actions: • Things that should be done every time a page loads • Things that should be done when the page is closed • Action that should be performed when a user clicks a button • Content that should be verified when a user inputs data • And more ..
  • 21. EVENTS Many different methods can be used to let JavaScript work with events: • HTML event attributes can execute JavaScript code directly • HTML event attributes can call JavaScript functions • You can assign your own event handler functions to HTML elements • You can prevent events from being sent or being handled • And more ...
  • 22. DATE OBJECTS Whenever you don’t have a date, and really would like one, JavaScript can help: you can create a new Date object
  • 23. DATE OBJECTS Method Description getFullYear() Get the year as a four digit number (yyyy) getMonth() Get the month as a number (0- 11) getDate() Get the day as a number (1-31) getHours() Get the hour (0-23) getMinutes() Get the minute (0-59) getSeconds() Get the second (0-59) getMilliseconds() Get the millisecond (0-999) getTime() Get the time (milliseconds since January 1, 1970) getDay() Get the weekday as a number (0-6)
  • 24. DATE OBJECTS Date object methods: getMonth() and getDay() are 0 indexed!
  • 25. STRICT MODE • Strict mode makes it easier to write "secure" JavaScript • Strict mode changes previously accepted "bad syntax" into real errors • As an example, in normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable • In normal JavaScript, a developer will not receive any error feedback assigning values to non- writable properties • In strict mode, any assignment to a non-writable property, a getter-only property, a non-existing
  • 26. STRICT MODE • Strict mode is declared by adding "use strict"; to the beginning of a script or a function • Declared at the beginning of a script, it has global scope • Declared inside a function, it has local scope
  • 28. ERROR HANDLING • The try statement allows you to define a block of code to be tested for errors while it is being executed • The catch statement allows you to define a block of code to be executed, if an error occurs in the try block • The throw statement allows you to create a custom error • The finally statement lets you execute code,
  • 30. DEBUGGING • Programming code might contain syntax errors, or logical errors • Many of these errors are difficult to diagnose • Often, when programming code contains errors, nothing will happen. There are no error messages, and you will get no indications where to search for errors • Searching for (and fixing) errors in programming code is called code debugging
  • 31. DEBUGGING • Debugging is not easy. But fortunately, all modern browsers have a built-in JavaScript debugger • Built-in debuggers can be turned on and off, forcing errors to be reported to the user • With a debugger, you can also set breakpoints (places where code execution can be stopped), and examine variables while the code is executing
  • 32. DEBUGGING • Normally, you activate debugging in your browser with F12, and selecting "Console" in the debugger menu • Otherwise follow these steps Chrome • From the menu, select "More tools" • From tools, choose "Developer tools" • Finally, select Console. Firefox • From the menu, select "Web Developer" • Finally, select "Web Console" Edge • From the menu, select "Developer Tools" • Finally, select "Console" Opera • From the menu, select "Developer" • From "Developer", select "Developer tools" • Finally, select "Console" Safari • Go to Safari, Preferences, Advanced in the main menu • Check "Enable Show Develop menu in menu bar" • When the new option "Develop" appears in the menu: Choose "Show Error Console"
  • 33. DEBUGGING • If your browser supports debugging, you can use console.log() to display JavaScript values in the debugger window
  • 34. DEBUGGING • If your browser supports debugging, you can use console.log() to display JavaScript values in the debugger window
  • 35. DEBUGGING • If your browser supports debugging, you can use console.log() to display JavaScript values in the debugger window
  • 36. DEBUGGING • In the debugger window, you can set breakpoints in the JavaScript code • At each breakpoint, JavaScript will stop executing, and let you examine JavaScript values • You can step over or step into functions and watch variables • After examining values, you can resume the execution of code (typically with a play button)
  • 38. DEBUGGING • The debugger keyword stops the execution of JavaScript, and calls (if available) the debugging function • This has the same function as setting a breakpoint in the debugger • If no debugging is available, the debugger statement has no effect
  • 40. STYLE GUIDES & CODING CONVENTIONS Coding conventions are style guidelines for programming. They typically cover: • Naming and declaration rules for variables and functions • Rules for the use of white space, indentation, and comments • Programming practices and principles Coding conventions secure quality: • Improve code readability • Make code maintenance easier
  • 41. STYLE GUIDES & CODING CONVENTIONS Code blocks • The opening curly brackets { of the corresponding code block start on the same line as the statement they belong to but end on a new line • Indent code to represent code blocks ownership
  • 42. STYLE GUIDES & CODING CONVENTIONS Variable Names • Use camelCase to name your variables • All variable names start with a letter (except jQuery DOM cache variables, which start with $ – will be covered in C11) • Remember that hyphens (-) are not allowed in JavaScript
  • 43. STYLE GUIDES & CODING CONVENTIONS • Always put spaces around operators and after commas
  • 44. STYLE GUIDES & CODING CONVENTIONS • General rules for simple statements: • Always end a simple statement with a semicolon • General rules for complex (compound) statements: • Put the opening bracket at the end of the first line • Use one space before the opening bracket • Put the closing bracket on a new line, without leading spaces • Do not end a complex statement with a semicolon
  • 45. STYLE GUIDES & CODING CONVENTIONS General rules for object definitions: • Place the opening bracket on the same line as the object name • Use colon plus one space between each property and its value • Use quotes around string values, not around numeric values • Do not add a comma after the last property-value pair • Place the closing bracket on a new line, without leading spaces
  • 46. STYLE GUIDES & CODING CONVENTIONS General rules for object definitions:
  • 47. STYLE GUIDES & CODING CONVENTIONS • For readability, avoid lines longer than 80 characters • If a JavaScript statement does not fit on one line, the best place to break it, is after an operator or a comma
  • 48. STYLE GUIDES & CODING CONVENTIONS Loading JavaScript in HTML • Use simple syntax for loading external scripts (the type attribute is not necessary)
  • 49. STYLE GUIDES & CODING CONVENTIONS • A consequence of using "untidy" HTML styles, might result in JavaScript errors • These two JavaScript statements will produce different results: • If possible, use the same naming convention (as JavaScript) in HTML
  • 50. STYLE GUIDES & CODING CONVENTIONS File extensions • HTML files should have a .html extension (not .htm). • CSS files should have a .css extension. • JavaScript files should have a .js extension File names • Use Lower Case File Names • Most web servers (Apache, Unix) are case sensitive about file names (london.jpg cannot be accessed as London.jpg) • Other web servers (Microsoft, IIS) are not case sensitive (london.jpg can be accessed as London.jpg or london.jpg) • If you use a mix of upper and lower case, you have to be extremely consistent • If you move from a case insensitive, to a case sensitive server, even small errors can break your web site • To avoid these problems, always use lower case file names (if
  • 51. STYLE GUIDES & CODING CONVENTIONS Performance • Coding conventions are not used by computers. Most rules have little impact on the execution of programs • Indentation and extra spaces are not significant in small scripts • For code in development, readability should be preferred. Larger production scripts should be minified
  • 52. BEST PRACTICES Avoid Global Variables • Minimize the use of global variables • This includes all data types, objects, and functions • Global variables and functions can be overwritten by other scripts • Use local variables instead
  • 53. BEST PRACTICES Always Declare Local Variables • All variables used in a function should be declared as local variables • Local variables must be declared with the var keyword or the let keyword, otherwise they will become global variables • Strict mode does not allow undeclared variables
  • 54. BEST PRACTICES Declarations on Top • It is a good coding practice to put all declarations at the top of each script or function • This will: • Give cleaner code • Provide a single place to look for local variables • Make it easier to avoid unwanted (implied) global variables • Reduce the possibility of unwanted re-declarations
  • 55. BEST PRACTICES Initialize Variables • It is a good coding practice to initialize variables when you declare them • This will: • Give cleaner code • Provide a single place to initialize variables • Avoid undefined values
  • 56. BEST PRACTICES Never Declare Number, String, or Boolean Objects • Always treat numbers, strings, or booleans as primitive values, not as objects • Declaring these types as objects, slows down execution speed, and produces nasty side effects
  • 57. BEST PRACTICES Don't Use new Object() • Use {} instead of new Object() • Use "" instead of new String() • Use 0 instead of new Number() • Use false instead of new Boolean() • Use [] instead of new Array() • Use /()/ instead of new RegExp() • Use function (){} instead of new Function()
  • 58. BEST PRACTICES Use === Comparison • The == comparison operator always converts (to matching types) before comparison • The === operator forces comparison of values and type
  • 59. BEST PRACTICES Use Parameter Defaults • If a function is called with a missing argument, the value of the missing argument is set to undefined • Undefined values can break your code. It is a good habit to assign default values to arguments Default parameter values
  • 60. BEST PRACTICES End your switches with defaults • Always end your switch statements with a default. Even if you think there is no need for it
  • 61. BEST PRACTICES Use you IDE to automatically format your code • “Smart” IDEs (Visual Studio) automatically format code according to most common formatting guidelines • To manually force Visual Studio 201x to reformat code use Ctrl+K, D • To manually force Visual Studio Code to reformat code use Alt+Shift+F
  • 62. COMMON MISTAKES Accidentally Using the Assignment Operator • JavaScript programs may generate unexpected results if a programmer accidentally uses an assignment operator (=), instead of a comparison operator (== or ===) in an if statement
  • 63. COMMON MISTAKES Expecting Loose Comparison • In regular comparison, data type does not matter • switch case expressions use strict comparison
  • 64. COMMON MISTAKES Misunderstanding Floats • All numbers in JavaScript are stored as 64-bits Floating point numbers (Floats). • All programming languages, including JavaScript, have difficulties with precise floating point values
  • 65. COMMON MISTAKES Breaking a JavaScript String • JavaScript will allow you to break a statement into two lines but, breaking a statement in the middle of a string will not work • You must use a "backslash" if you must break a statement in a string
  • 66. COMMON MISTAKES Misplacing semicolon • Because of a misplaced semicolon, this code block will execute regardless of the value of x
  • 67. COMMON MISTAKES • JavaScript tries to add automatic semicolons where it considers them to be missing • This can cause unexpected behavior if code blocks are defined with line breaks JS will insert an automatic semicolon
  • 68. PERFORMANCE TIPS Reduce Activity in Loops • Each statement in a loop, including the for statement, is executed for each iteration of the loop • Statements or assignments that can be placed outside the loop will make the loop run faster Bad Good
  • 69. PERFORMANCE TIPS Reduce DOM Access • Accessing the HTML DOM is very slow, compared to other JavaScript statements • If you expect to access a DOM element several times, access it once, and use it as a local variable (called caching the DOM)
  • 70. PERFORMANCE TIPS Avoid Unnecessary Variables • Don't create new variables if you don't plan to save values Bad Good
  • 71. PERFORMANCE TIPS Delay JavaScript loading • Putting your scripts at the bottom of the page body lets the browser load the page first • While a script is downloading, the browser will not start any other downloads. In addition all parsing and rendering activity might be blocked • The HTTP specification defines that browsers should not download more than two components in parallel
  • 72. PERFORMANCE TIPS Delay JavaScript loading • An alternative is to use defer in the script tag. The defer attribute specifies that the script should be executed after the page has finished parsing, but it only works for external scripts. • If possible, you can add your script to the page by code, after the page has loaded
  • 73. BROWSER SUPPORT Version Browser Chrome Firefox IE Edge Safari Opera ES5 (2009) 23 21 9 10 6 15 ECMAScri pt 2015 51 54 N/A 14 10 38 ECMAScri pt 2016 68 N/A N/A N/A N/A 55
  • 75. COURSES Available online at: http://www.ael.utcluj.ro/ Information for Students -> Courses -> Web Technologies