SlideShare a Scribd company logo
Web Programming
JavaScript
 JavaScript is a programming language used to make web
pages interactive. It runs on your visitor's computer and
doesn't require constant downloads from your website.
 JavaScript is the programming language of HTML and the
Web.
JS Where To
 JavaScript can be placed in the <body> and the
<head> sections of an HTML page.
The <script> Tag
 In HTML, JavaScript code must be inserted between <script> and </script>
tags.
JavaScript Functions and Events
 A JavaScript function is a block of JavaScript code, that can be executed when
"asked" for.
 A function can be executed when an event occurs, like when the user clicks a
button.
JavaScript in <head> or <body>
 You can place any number of scripts in an HTML document.
 Scripts can be placed in the <body>, or in the <head>
section of an HTML page, or in both.
JavaScript in <head>
JavaScript in <body>
External JavaScript
 Scripts can also be placed in external files:
 External scripts are practical when the same code is used in many different
web pages.
 JavaScript files have the file extension .js.
External JavaScript
 To use an external script, put the name of the script file in the src (source)
attribute of a <script> tag:
External JavaScript Advantages
 Placing JavaScripts in external files has some advantages:
 It separates HTML and code
 It makes HTML and JavaScript easier to read and maintain
 Cached JavaScript files can speed up page loads
JS Output
 JavaScript can "display" data in different ways:
 Writing into an alert box, using window.alert().
 Writing into the HTML output using document.write().
 Writing into an HTML element, using innerHTML.
 Writing into the browser console, using console.log().
Using window.alert()
 You can use an alert box to display data:
Using document.write()
Using document.write()
 Using document.write() after an HTML document is fully loaded, will delete all
existing HTML:
Using innerHTML
 To access an HTML element, JavaScript can use the
document.getElementById(id) method.
 The id attribute defines the HTML element. The innerHTML property defines
the HTML content:
Using console.log()
 In your browser, you can use the console.log() method to display data.
 Activate the browser console with F12, and select "Console" in the menu.
JavaScript Programs
 A computer program is a list of "instructions" to be "executed" by the computer.
 In a programming language, these program instructions are called statements.
 JavaScript is a programming language.
 JavaScript statements are separated by semicolons.
JavaScript Statements
 JavaScript statements are composed of:
 Values
 Operators
 Expressions
 Keywords
 Comments.
JavaScript Values
 The JavaScript syntax defines two types of values:
 Fixed values
 variable values.
 Fixed values are called literals.
 Variable values are called variables.
JavaScript Literals
 The most important rules for writing fixed values are:
 Numbers are written with or without decimals:
 Strings are text, written within double or single quotes:
JavaScript Variables
 In a programming language, variables are used to store data values.
 JavaScript uses the var keyword to declare variables.
 An equal sign is used to assign values to variables.
 In this example, x is defined as a variable. Then, x is assigned (given) the
value 6:
JavaScript Operators
 JavaScript uses an assignment operator ( = ) to assign values to variables:
 JavaScript uses arithmetic operators ( + - * / ) to compute values:
JavaScript Expressions
 An expression is a combination of values, variables, and operators, which
computes to a value.
 The computation is called an evaluation.
 For example, 5 * 10 evaluates to 50:
 Expressions can also contain variable values:
 The values can be of various types, such as numbers and strings.
 For example, "John" + " " + "Doe", evaluates to "John Doe":
JavaScript Keywords
 JavaScript keywords are used to identify actions to be performed.
 The var keyword tells the browser to create a new variable:
JavaScript Comments
 Not all JavaScript statements are "executed".
 Code after double slashes // or between /* and */ is treated as a comment.
 Comments are ignored, and will not be executed:
JavaScript Identifiers
 Identifiers are names.
 In JavaScript, identifiers are used to name variables (and keywords, and
functions, and labels).
 The rules for legal names are much the same in most programming languages.
 In JavaScript, the first character must be a letter, an underscore (_), or a
dollar sign ($).
 Subsequent characters may be letters, digits, underscores, or dollar signs.
JavaScript is Case Sensitive
 All JavaScript identifiers are case sensitive.
 The variables lastName and lastname, are two different variables.
 JavaScript does not interpret VAR or Var as the keyword var.
JavaScript and Camel Case
Historically, programmers have used three ways of joining multiple words into one
variable name:
 Hyphens:
first-name, last-name, master-card, inter-city.
 Underscore:
first_name, last_name, master_card, inter_city.
 Camel Case:
FirstName, LastName, MasterCard, InterCity.
In programming languages, especially in JavaScript, camel case often starts with a
lowercase letter:
 firstName, lastName, masterCard, interCity.
JavaScript Statements
 JavaScript statements are "instructions" to be "executed" by the web browser.
 This statement tells the browser to write "Hello Dolly." inside an HTML
element with id="demo":
JavaScript Programs
 Most JavaScript programs contain many JavaScript statements.
 The statements are executed, one by one, in the same order as they are
written.
 In this example x, y, and z are given values, and finally z is displayed:
Semicolons ;
 Semicolons separate JavaScript statements.
 Add a semicolon at the end of each executable statement:
 When separated by semicolons, multiple statements on one line are allowed:
JavaScript White Space
 JavaScript ignores multiple spaces. You can add white space to your script to
make it more readable.
 The following lines are equivalent:
 A good practice is to put spaces around operators ( = + - * / ):
JavaScript Line Length and Line Breaks
 For best readability, programmers often like to avoid code 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:
JavaScript Code Blocks
 JavaScript statements can be grouped together in code blocks, inside curly
brackets {...}.
 The purpose of code blocks is to define statements to be executed together.
 One place you will find statements grouped together in blocks, are in
JavaScript functions:
JavaScript Comments – Single Line
Comments
 Single line comments start with //.
 Any text between // and the end of the line will be ignored by JavaScript (will
not be executed).
 This example uses a single-line comment before each code line:
JavaScript Comments – Single Line
Comments
 This example uses a single line comment at the end of each line to explain the
code:
Multi-line Comments
 Multi-line comments start with /* and end with */.
 Any text between /* and */ will be ignored by JavaScript.
 This example uses a multi-line comment (a comment block) to explain the
code:
Using Comments to Prevent Execution
 Using comments to prevent execution of code is suitable for code testing.
 Adding // in front of a code line changes the code lines from an executable
line to a comment.
 This example uses // to prevent execution of one of the code lines:
Using Comments to Prevent Execution
 This example uses a comment block to prevent execution of multiple lines:
JavaScript Variables
 JavaScript variables are containers for storing data values.
 In this example, x, y, and z, are variables:
 From the example above, you can expect:
x stores the value 5
y stores the value 6
z stores the value 11
Much Like Algebra
 In this example, price1, price2, and total, are variables:
 we use variables (like price1) to hold values.
 we use variables in expressions (total = price1 + price2).
JavaScript Data Types
 JavaScript variables can hold numbers like 100, and text
values like "John Doe".
 In programming, text values are called text strings.
 JavaScript can handle many types of data, but for now,
just think of numbers and strings.
 Strings are written inside double or single quotes.
Numbers are written without quotes.
 If you put quotes around a number, it will be treated as a
text string.
Declaring (Creating) JavaScript Variables
 Creating a variable in JavaScript is called "declaring" a variable.
 You declare a JavaScript variable with the var keyword:
 After the declaration, the variable has no value. (Technically it has the value
of undefined)
 To assign a value to the variable, use the equal sign:
Declaring (Creating) JavaScript Variables
 You can also assign a value to the variable when you declare it:
 we create a variable called carName and assign the value "Volvo" to it.
 Then we "output" the value inside an HTML paragraph with id="demo":
One Statement, Many Variables
 You can declare many variables in one statement.
 Start the statement with var and separate the variables by comma:
 A declaration can span multiple lines:
Value = undefined
 In computer programs, variables are often declared without a value. The value
can be something that has to be calculated, or something that will be provided
later, like user input.
 A variable declared without a value will have the value undefined.
 The variable carName will have the value undefined after the execution of this
statement:
Re-Declaring JavaScript Variables
 If you re-declare a JavaScript variable, it will not lose its value.
 The variable carName will still have the value "Volvo" after the execution of
these statements:
JavaScript Arithmetic
 As with algebra, you can do arithmetic with JavaScript variables, using
operators like = and +:
 You can also add strings, but strings will be concatenated (added end-to-end):
JavaScript Operators
Arithmetic Operators
 Arithmetic operators are used to perform arithmetic on numbers (literals or
variables).
JavaScript Operators
Arithmetic Operators
 The addition operator (+) adds numbers:  The multiplication operator (*) multiplies
numbers.
JavaScript Assignment Operators
 Assignment operators assign values to JavaScript variables.
JavaScript String Operators
 The + operator can also be used to add (concatenate) strings.
Adding Strings and Numbers
 Adding two numbers, will return the sum, but adding a number and a string
will return a string:
JavaScript Comparison and Logical Operators
JavaScript Data Types
 JavaScript variables can hold many data types: numbers, strings, arrays,
objects and more:
JavaScript Has Dynamic Types
 JavaScript has dynamic types. This means that the same variable can be used
as different types:
JavaScript Strings
 A string (or a text string) is a series of characters like "John Doe".
 Strings are written with quotes. You can use single or double quotes:
JavaScript Numbers
 JavaScript has only one type of numbers.
 Numbers can be written with, or without decimals:
 Extra large or extra small numbers can be written with scientific (exponential)
notation:
JavaScript Booleans
 Booleans can only have two values: true or false.
JavaScript Arrays
 JavaScript arrays are written with square brackets.
 Array items are separated by commas.
 The following code declares (creates) an array called cars, containing three
items (car names):
Array indexes are zero-based, which means the first item is [0], second is [1], and
so on.
JavaScript Objects
 JavaScript objects are written with curly braces.
 Object properties are written as name:value pairs, separated by commas.
Empty Values
 An empty value has nothing to do with undefined.
 An empty string variable has both a value and a type.

More Related Content

What's hot (20)

PPTX
Presentation on C++ Programming Language
satvirsandhu9
 
PPT
Control Structures
Ghaffar Khan
 
PPT
Javascript arrays
Hassan Dar
 
PPT
Introduction to c#
OpenSource Technologies Pvt. Ltd.
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPTX
Typecasting in c
Tushar Shende
 
PPSX
Introduction to Html5
www.netgains.org
 
PPT
Control structure C++
Anil Kumar
 
PPT
Looping statements in Java
Jin Castor
 
PPTX
PHP FUNCTIONS
Zeeshan Ahmed
 
PDF
Methods in Java
Jussi Pohjolainen
 
PPT
Operator Overloading
Nilesh Dalvi
 
PPTX
OOPS Basics With Example
Thooyavan Venkatachalam
 
PPTX
HTML/HTML5
People Strategists
 
PDF
PHP Loops and PHP Forms
M.Zalmai Rahmani
 
PPTX
How Hashmap works internally in java
Ramakrishna Joshi
 
PPT
Basics of C programming
avikdhupar
 
PPTX
Our presentation on algorithm design
Nahid Hasan
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPTX
C# Tutorial
Jm Ramos
 
Presentation on C++ Programming Language
satvirsandhu9
 
Control Structures
Ghaffar Khan
 
Javascript arrays
Hassan Dar
 
classes and objects in C++
HalaiHansaika
 
Typecasting in c
Tushar Shende
 
Introduction to Html5
www.netgains.org
 
Control structure C++
Anil Kumar
 
Looping statements in Java
Jin Castor
 
PHP FUNCTIONS
Zeeshan Ahmed
 
Methods in Java
Jussi Pohjolainen
 
Operator Overloading
Nilesh Dalvi
 
OOPS Basics With Example
Thooyavan Venkatachalam
 
HTML/HTML5
People Strategists
 
PHP Loops and PHP Forms
M.Zalmai Rahmani
 
How Hashmap works internally in java
Ramakrishna Joshi
 
Basics of C programming
avikdhupar
 
Our presentation on algorithm design
Nahid Hasan
 
Inheritance in c++
Vineeta Garg
 
C# Tutorial
Jm Ramos
 

Viewers also liked (10)

PPT
Programming paradigm and web programming
Mohammad Kamrul Hasan
 
ODP
Web Server-Side Programming Techniques
guest8899ec02
 
PPTX
How To Become A Php Geek
Kazi Mohammad Ekram
 
PPT
Web Server Programming - Chapter 1
Nicole Ryan
 
PDF
Php & mysql course syllabus
Papitha Velumani
 
PPT
PHP MySQL Workshop - facehook
Shashank Skills Academy
 
PPT
Beginners PHP Tutorial
alexjones89
 
ODP
PHP Web Programming
Muthuselvam RS
 
PPT
Class 6 - PHP Web Programming
Ahmed Swilam
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
Programming paradigm and web programming
Mohammad Kamrul Hasan
 
Web Server-Side Programming Techniques
guest8899ec02
 
How To Become A Php Geek
Kazi Mohammad Ekram
 
Web Server Programming - Chapter 1
Nicole Ryan
 
Php & mysql course syllabus
Papitha Velumani
 
PHP MySQL Workshop - facehook
Shashank Skills Academy
 
Beginners PHP Tutorial
alexjones89
 
PHP Web Programming
Muthuselvam RS
 
Class 6 - PHP Web Programming
Ahmed Swilam
 
Ad

Similar to Web programming (20)

PPTX
04-JS.pptx
RazanMazen4
 
PPTX
04-JS.pptx
RazanMazen4
 
PPTX
04-JS.pptx
RazanMazen4
 
PPTX
Javascript Tlabs
msneha
 
PPTX
Java script
Shyam Khant
 
PPTX
JavaScript Fundamentals & JQuery
Jamshid Hashimi
 
PPTX
Java script
Jay Patel
 
PPTX
Learning space presentation1 learn Java script
engmk83
 
PPTX
Unit III.pptx IT3401 web essentials presentatio
lakshitakumar291
 
PDF
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
pkaviya
 
PDF
Ch3- Java Script.pdf
HASENSEID
 
PPTX
javascript client side scripting la.pptx
lekhacce
 
PPTX
Chapter 3 INTRODUCTION TO JAVASCRIPT S.pptx
KelemAlebachew
 
PPTX
Lecture 5 javascript
Mujtaba Haider
 
PDF
javascriptPresentation.pdf
wildcat9335
 
PDF
Iwt note(module 2)
SANTOSH RATH
 
PPTX
Java script
Sukrit Gupta
 
PDF
internet Chapter 4-JavascripPrepare a ppt of video compression techniques bas...
wabii3179com
 
PPTX
Java script
bosybosy
 
PPTX
JavaScript_III.pptx
rashmiisrani1
 
04-JS.pptx
RazanMazen4
 
04-JS.pptx
RazanMazen4
 
04-JS.pptx
RazanMazen4
 
Javascript Tlabs
msneha
 
Java script
Shyam Khant
 
JavaScript Fundamentals & JQuery
Jamshid Hashimi
 
Java script
Jay Patel
 
Learning space presentation1 learn Java script
engmk83
 
Unit III.pptx IT3401 web essentials presentatio
lakshitakumar291
 
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
pkaviya
 
Ch3- Java Script.pdf
HASENSEID
 
javascript client side scripting la.pptx
lekhacce
 
Chapter 3 INTRODUCTION TO JAVASCRIPT S.pptx
KelemAlebachew
 
Lecture 5 javascript
Mujtaba Haider
 
javascriptPresentation.pdf
wildcat9335
 
Iwt note(module 2)
SANTOSH RATH
 
Java script
Sukrit Gupta
 
internet Chapter 4-JavascripPrepare a ppt of video compression techniques bas...
wabii3179com
 
Java script
bosybosy
 
JavaScript_III.pptx
rashmiisrani1
 
Ad

More from Leo Mark Villar (12)

PPTX
Date security identifcation and authentication
Leo Mark Villar
 
PPTX
Date security security principles
Leo Mark Villar
 
PPTX
Data security authorization and access control
Leo Mark Villar
 
PPTX
Date security introduction
Leo Mark Villar
 
PPTX
Data security auditing and accountability
Leo Mark Villar
 
PPTX
Computer fundamentals-internet p2
Leo Mark Villar
 
PPTX
Computer fundamentals-internet p1
Leo Mark Villar
 
PPTX
Html
Leo Mark Villar
 
PPTX
Team foundation server
Leo Mark Villar
 
PPTX
Microsoft office 2013
Leo Mark Villar
 
PPTX
Sql performance tuning
Leo Mark Villar
 
PPTX
Angular js
Leo Mark Villar
 
Date security identifcation and authentication
Leo Mark Villar
 
Date security security principles
Leo Mark Villar
 
Data security authorization and access control
Leo Mark Villar
 
Date security introduction
Leo Mark Villar
 
Data security auditing and accountability
Leo Mark Villar
 
Computer fundamentals-internet p2
Leo Mark Villar
 
Computer fundamentals-internet p1
Leo Mark Villar
 
Team foundation server
Leo Mark Villar
 
Microsoft office 2013
Leo Mark Villar
 
Sql performance tuning
Leo Mark Villar
 
Angular js
Leo Mark Villar
 

Recently uploaded (20)

PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 

Web programming

  • 2. JavaScript  JavaScript is a programming language used to make web pages interactive. It runs on your visitor's computer and doesn't require constant downloads from your website.  JavaScript is the programming language of HTML and the Web.
  • 3. JS Where To  JavaScript can be placed in the <body> and the <head> sections of an HTML page.
  • 4. The <script> Tag  In HTML, JavaScript code must be inserted between <script> and </script> tags.
  • 5. JavaScript Functions and Events  A JavaScript function is a block of JavaScript code, that can be executed when "asked" for.  A function can be executed when an event occurs, like when the user clicks a button.
  • 6. JavaScript in <head> or <body>  You can place any number of scripts in an HTML document.  Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both.
  • 9. External JavaScript  Scripts can also be placed in external files:  External scripts are practical when the same code is used in many different web pages.  JavaScript files have the file extension .js.
  • 10. External JavaScript  To use an external script, put the name of the script file in the src (source) attribute of a <script> tag:
  • 11. External JavaScript Advantages  Placing JavaScripts in external files has some advantages:  It separates HTML and code  It makes HTML and JavaScript easier to read and maintain  Cached JavaScript files can speed up page loads
  • 12. JS Output  JavaScript can "display" data in different ways:  Writing into an alert box, using window.alert().  Writing into the HTML output using document.write().  Writing into an HTML element, using innerHTML.  Writing into the browser console, using console.log().
  • 13. Using window.alert()  You can use an alert box to display data:
  • 15. Using document.write()  Using document.write() after an HTML document is fully loaded, will delete all existing HTML:
  • 16. Using innerHTML  To access an HTML element, JavaScript can use the document.getElementById(id) method.  The id attribute defines the HTML element. The innerHTML property defines the HTML content:
  • 17. Using console.log()  In your browser, you can use the console.log() method to display data.  Activate the browser console with F12, and select "Console" in the menu.
  • 18. JavaScript Programs  A computer program is a list of "instructions" to be "executed" by the computer.  In a programming language, these program instructions are called statements.  JavaScript is a programming language.  JavaScript statements are separated by semicolons.
  • 19. JavaScript Statements  JavaScript statements are composed of:  Values  Operators  Expressions  Keywords  Comments.
  • 20. JavaScript Values  The JavaScript syntax defines two types of values:  Fixed values  variable values.  Fixed values are called literals.  Variable values are called variables.
  • 21. JavaScript Literals  The most important rules for writing fixed values are:  Numbers are written with or without decimals:  Strings are text, written within double or single quotes:
  • 22. JavaScript Variables  In a programming language, variables are used to store data values.  JavaScript uses the var keyword to declare variables.  An equal sign is used to assign values to variables.  In this example, x is defined as a variable. Then, x is assigned (given) the value 6:
  • 23. JavaScript Operators  JavaScript uses an assignment operator ( = ) to assign values to variables:  JavaScript uses arithmetic operators ( + - * / ) to compute values:
  • 24. JavaScript Expressions  An expression is a combination of values, variables, and operators, which computes to a value.  The computation is called an evaluation.  For example, 5 * 10 evaluates to 50:  Expressions can also contain variable values:  The values can be of various types, such as numbers and strings.  For example, "John" + " " + "Doe", evaluates to "John Doe":
  • 25. JavaScript Keywords  JavaScript keywords are used to identify actions to be performed.  The var keyword tells the browser to create a new variable:
  • 26. JavaScript Comments  Not all JavaScript statements are "executed".  Code after double slashes // or between /* and */ is treated as a comment.  Comments are ignored, and will not be executed:
  • 27. JavaScript Identifiers  Identifiers are names.  In JavaScript, identifiers are used to name variables (and keywords, and functions, and labels).  The rules for legal names are much the same in most programming languages.  In JavaScript, the first character must be a letter, an underscore (_), or a dollar sign ($).  Subsequent characters may be letters, digits, underscores, or dollar signs.
  • 28. JavaScript is Case Sensitive  All JavaScript identifiers are case sensitive.  The variables lastName and lastname, are two different variables.  JavaScript does not interpret VAR or Var as the keyword var.
  • 29. JavaScript and Camel Case Historically, programmers have used three ways of joining multiple words into one variable name:  Hyphens: first-name, last-name, master-card, inter-city.  Underscore: first_name, last_name, master_card, inter_city.  Camel Case: FirstName, LastName, MasterCard, InterCity. In programming languages, especially in JavaScript, camel case often starts with a lowercase letter:  firstName, lastName, masterCard, interCity.
  • 30. JavaScript Statements  JavaScript statements are "instructions" to be "executed" by the web browser.  This statement tells the browser to write "Hello Dolly." inside an HTML element with id="demo":
  • 31. JavaScript Programs  Most JavaScript programs contain many JavaScript statements.  The statements are executed, one by one, in the same order as they are written.  In this example x, y, and z are given values, and finally z is displayed:
  • 32. Semicolons ;  Semicolons separate JavaScript statements.  Add a semicolon at the end of each executable statement:  When separated by semicolons, multiple statements on one line are allowed:
  • 33. JavaScript White Space  JavaScript ignores multiple spaces. You can add white space to your script to make it more readable.  The following lines are equivalent:  A good practice is to put spaces around operators ( = + - * / ):
  • 34. JavaScript Line Length and Line Breaks  For best readability, programmers often like to avoid code 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:
  • 35. JavaScript Code Blocks  JavaScript statements can be grouped together in code blocks, inside curly brackets {...}.  The purpose of code blocks is to define statements to be executed together.  One place you will find statements grouped together in blocks, are in JavaScript functions:
  • 36. JavaScript Comments – Single Line Comments  Single line comments start with //.  Any text between // and the end of the line will be ignored by JavaScript (will not be executed).  This example uses a single-line comment before each code line:
  • 37. JavaScript Comments – Single Line Comments  This example uses a single line comment at the end of each line to explain the code:
  • 38. Multi-line Comments  Multi-line comments start with /* and end with */.  Any text between /* and */ will be ignored by JavaScript.  This example uses a multi-line comment (a comment block) to explain the code:
  • 39. Using Comments to Prevent Execution  Using comments to prevent execution of code is suitable for code testing.  Adding // in front of a code line changes the code lines from an executable line to a comment.  This example uses // to prevent execution of one of the code lines:
  • 40. Using Comments to Prevent Execution  This example uses a comment block to prevent execution of multiple lines:
  • 41. JavaScript Variables  JavaScript variables are containers for storing data values.  In this example, x, y, and z, are variables:  From the example above, you can expect: x stores the value 5 y stores the value 6 z stores the value 11
  • 42. Much Like Algebra  In this example, price1, price2, and total, are variables:  we use variables (like price1) to hold values.  we use variables in expressions (total = price1 + price2).
  • 43. JavaScript Data Types  JavaScript variables can hold numbers like 100, and text values like "John Doe".  In programming, text values are called text strings.  JavaScript can handle many types of data, but for now, just think of numbers and strings.  Strings are written inside double or single quotes. Numbers are written without quotes.  If you put quotes around a number, it will be treated as a text string.
  • 44. Declaring (Creating) JavaScript Variables  Creating a variable in JavaScript is called "declaring" a variable.  You declare a JavaScript variable with the var keyword:  After the declaration, the variable has no value. (Technically it has the value of undefined)  To assign a value to the variable, use the equal sign:
  • 45. Declaring (Creating) JavaScript Variables  You can also assign a value to the variable when you declare it:  we create a variable called carName and assign the value "Volvo" to it.  Then we "output" the value inside an HTML paragraph with id="demo":
  • 46. One Statement, Many Variables  You can declare many variables in one statement.  Start the statement with var and separate the variables by comma:  A declaration can span multiple lines:
  • 47. Value = undefined  In computer programs, variables are often declared without a value. The value can be something that has to be calculated, or something that will be provided later, like user input.  A variable declared without a value will have the value undefined.  The variable carName will have the value undefined after the execution of this statement:
  • 48. Re-Declaring JavaScript Variables  If you re-declare a JavaScript variable, it will not lose its value.  The variable carName will still have the value "Volvo" after the execution of these statements:
  • 49. JavaScript Arithmetic  As with algebra, you can do arithmetic with JavaScript variables, using operators like = and +:  You can also add strings, but strings will be concatenated (added end-to-end):
  • 50. JavaScript Operators Arithmetic Operators  Arithmetic operators are used to perform arithmetic on numbers (literals or variables).
  • 51. JavaScript Operators Arithmetic Operators  The addition operator (+) adds numbers:  The multiplication operator (*) multiplies numbers.
  • 52. JavaScript Assignment Operators  Assignment operators assign values to JavaScript variables.
  • 53. JavaScript String Operators  The + operator can also be used to add (concatenate) strings.
  • 54. Adding Strings and Numbers  Adding two numbers, will return the sum, but adding a number and a string will return a string:
  • 55. JavaScript Comparison and Logical Operators
  • 56. JavaScript Data Types  JavaScript variables can hold many data types: numbers, strings, arrays, objects and more:
  • 57. JavaScript Has Dynamic Types  JavaScript has dynamic types. This means that the same variable can be used as different types:
  • 58. JavaScript Strings  A string (or a text string) is a series of characters like "John Doe".  Strings are written with quotes. You can use single or double quotes:
  • 59. JavaScript Numbers  JavaScript has only one type of numbers.  Numbers can be written with, or without decimals:  Extra large or extra small numbers can be written with scientific (exponential) notation:
  • 60. JavaScript Booleans  Booleans can only have two values: true or false.
  • 61. JavaScript Arrays  JavaScript arrays are written with square brackets.  Array items are separated by commas.  The following code declares (creates) an array called cars, containing three items (car names): Array indexes are zero-based, which means the first item is [0], second is [1], and so on.
  • 62. JavaScript Objects  JavaScript objects are written with curly braces.  Object properties are written as name:value pairs, separated by commas.
  • 63. Empty Values  An empty value has nothing to do with undefined.  An empty string variable has both a value and a type.