Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to JavaScript

Similar presentations


Presentation on theme: "Introduction to JavaScript"— Presentation transcript:

1 Introduction to JavaScript
B. Ramamurthy 12/1/2018

2 Overview We will begin with simple exercises on HTML (UI)+JS (control/action)+CSS (style) We will focus on separation of the various components. Then we will drop the style (css) and focus on a few popular js libraries/frameworks such as jquery and angularjs (later) 12/1/2018

3 Structure – Style -- Interaction
HTML provides structure CSS provides style; CSS provides style attributes for the HTML selector tags Javascript (JS) provides control for interaction and operations JS provides functions that can be called to perform operations 12/1/2018

4 Exercise with all three components
<!DOCTYPE html> <head> <title> My first JavaScript </title> <style> h1{color: blue} </style> </head> <body> <h1> <script> document.write("Hello World!") </script> </h1> </body> </html> Head Style/css Body Script/js 12/1/2018

5 Separate files for style and scripts
<!DOCTYPE html> <head> <title> My first JavaScript </title> </head> <body> <h1> js function outside </h1> </body> </html> Head .css file <style> h1{color: blue} </style> Style/css Body <script src="myscripts.js"></script> .js file function mywrite() {document.write("Hello World!");} 12/1/2018

6 Moving CSS, JS to an external files
We can separate style elements in css file, behavioral elements can be moved to an external js file. This separation of concerns has resulted in explosion of javascript libraries (framework) and css style libraries. Large collection of superior and highly useful js libraries are available 12/1/2018

7 JS functions Javascript “script” consists of functions.
A function consists of function followed by the name of the function The statement that make up the function go next within curly brackets Example: function saySomething() { alert(“ We are learning basics of JS”); } 12/1/2018

8 Putting all together .html file .css file application Web browser
Javascript Libraries .html file .css file application Web browser Firefox/ Safari image and audio files .js file interprets displays Style(.css) Libraries Prepare/edit files 12/1/2018

9 Visualizations Great visualizations are not just informative but initiate conversations, explosion of free social media communications/messaging/instagramming etc. result in valuable free marketing to target customer segment Great visualization tells a story instantly Excel is de facto standard but it is designed as a data entry application and optimized for graphs/plots: not good for unstructured and social media data; look beyond excel tables and graphs Interactive visualization provides new modes of engagement previously impossible Opens up previously invisible aspects of data 12/1/2018

10 HTML5+DOM No matter what the backend, HTML and JavaScript are the technologies for all web developers for front end. 12/1/2018

11 Programming Concepts This chapter introduces the following programming concepts: Names, values, and variables Declarations Data types, numbers, string literals, and Booleans Assignment Expressions Conditionals

12 Identifiers and Their Rules
The letter sequence that makes up a variable’s name is called the identifier Identifiers have a particular form Identifiers must begin with a letter, followed by any sequence of letters, numerals, or the underscore symbol Identifiers are not allowed to contain spaces

13 Identifiers and Their Rules
Note two features of identifiers: The underscore symbol can be used as a word separator It makes identifiers more readable There is a “no spaces” rule Identifiers are case sensitive uppercase and lowercase letters are different

14 Variable Declaration Statement
var area, radius; This command declares that two identifiers (area, radius) will be used as variables

15 Rules for Declaring Variables
Every variable used must be declared JavaScript allows declaration statements anywhere in the list of statements Variable declarations announce what variables will be used in the program Declare variables first

16 Initializing a Declaration
Sometimes there is an initial value for identifiers JavaScript allows setting the initial value as part of the declaration This is called initializing the variable Declaring variables with initial values is written as: var taxRate = .088; var balanceDue = 0;

17 Strings Strings are a common kind of data
Strings are “sequences of keyboard characters” Notice that a string is always surrounded by single (') or double (") quotes Strings can initialize a declaration Strings are needed when manipulating text

18 Rules for Writing Strings in JavaScript
There are rules for writing strings in JavaScript: Strings must be surrounded by quotes, either single (') or double ("), which are not curly Most characters are allowed within quotes except: the return character ( ), backspace character, tab character, \, and two little used others Double quoted strings can contain single quotes, and vice versa

19 Rules for Writing Strings in JavaScript
There are rules for writing strings in JavaScript: The apostrophe (') is the same as the single quote Any number of characters is allowed in a string. The minimum number of characters in a string is zero (""), which is called the empty string

20 Strings To use double quotes in a string, enclose the string in single quotes: var answer = 'He said, "No!“ ‘ If our string contains single quotes, enclose it in double quotes: var book = "Guide to B&B's" Since the apostrophe is commonly used in possessives and contractions, use double quotes as the default

21 Escape Mechanisms For JavaScript, the escape symbol is the backslash (\) The escape sequences are converted to the single characters they represent when stored in the computer’s memory

22 Boolean Values Another kind of value is the Boolean value (Booleans)
There are only two Boolean values: true and false Boolean values are written as letter sequences, they are values, not identifiers or strings Booleans are used implicitly throughout the programming process

23 Relational Operators Relational operators make comparisons between numerical values The relationship between two numbers is tested The outcome of the comparison is a Boolean value of true or false The “equal to” relational operator (==) is a double equal sign The “not equal to” operator uses the !

24 Operator Overload Operator overload is a technical term meaning the “use of an operator with different data types” Strings to numbers or to Booleans Operators usually apply to a single data type 4 + 5 produces the numerical result of 9 If operands are strings, what does the + mean?

25 Concatenation When we use + with strings, it joins the strings by the operation of concatenation It just means that the two strings are placed together if we want them joined The meaning of + is overloaded: + to mean addition when operands are numeric + means concatenation when the operands are strings

26 if Statements and Their Flow of Control
if (waterTemp < 32) waterState = "Frozen"; The <Boolean expression> is called a predicate It is evaluated, resulting in a true or false outcome If the outcome is true, the <then-statement> is performed If the outcome is false, the <then-statement> is skipped

27 if/else Statements if (<Boolean expression>)
The if/else statement contain statements that will be executed when the condition’s outcome is false if (<Boolean expression>) <then-statement>; else <else-statement>;

28 Summary We looked at basic elements of JavaScript language.
These were introduced to illustrate the common constructs that JS has with many of the high level languages that you are familiar with. 12/1/2018


Download ppt "Introduction to JavaScript"

Similar presentations


Ads by Google