Introduction to JavaScript

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Introducing JavaScript
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
The Information School of the University of Washington Oct 20fit programming1 Programming Basics INFO/CSE 100, Fall 2006 Fluency in Information Technology.
Introduction to scripting
PHP : Hypertext Preprocessor
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Week 9 PHP Cookies and Session Introduction to JavaScript.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
Chapter 3: Data Types and Operators JavaScript - Introductory.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
Step 1: Starting an HTML Document: Right Click: new>text document.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Precedence Operators Error Types
Thinking about programming
Math operations 9/19/16.
Chapter 6 JavaScript: Introduction to Scripting
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Variables, Expressions, and IO
Thinking about programming
JavaScript Syntax and Semantics
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
PHP Introduction.
JavaScript.
Intro to PHP & Variables
Introduction to JavaScript
A second look at JavaScript
Number and String Operations
WEB PROGRAMMING JavaScript.
Introduction to JavaScript
INFO/CSE 100, Spring 2005 Fluency in Information Technology
T. Jumana Abu Shmais – AOU - Riyadh
Class07 PHP: loops MIS 3501 Jeremy Shafer Department of MIS
INFO/CSE 100, Spring 2006 Fluency in Information Technology
An Introduction to JavaScript
Programming Control Structures with JavaScript Part 2
Chapter 2: Introduction to C++.
An introduction to jQuery
Conditional Statements with JavaScript
Building Java Programs
Loops and Arrays in JavaScript
JavaScript objects, functions, and events
Introduction to JavaScript
Introduction to JavaScript
Tutorial 10: Programming with javascript
An introduction to jQuery
Introducing JavaScript
Javascript Chapter 19 and 20 5/3/2019.
An introduction to jQuery
Strings and Dates in JavaScript
Web Programming and Design
Presentation transcript:

Introduction to JavaScript MIS 2402 Jeremy Shafer Department of MIS Fox School of Business Temple University

Today’s class… Today’s class is a little different. It is a lecture and lab smushed together. We will introduce some basic concepts of the JavaScript language that we will build on next week. We will also stop periodically and run JavaScript code that illustrates a concept. So … before we do anything else, see intro_js.zip and get it set up in Visual Studio Code.

Agenda A quick review of the role of JavaScript An introduction to the developer console in Chrome An introduction to variables, and two simple data types Putting JavaScript to work doing some simple math Making our output more readable with the parseInt function and the toFixed method

The role of JavaScript HTML CSS JavaScript JavaScript is a general purpose language. It can be used to manipulate the HTML and CSS in the browser in response to an event. But today … we are going to just focus on JavaScript itself, separating it from HTML and CSS as much as possible.

The developer console When you preview intro_js/index.html all you will see is a message that says “See the web developer console. Assuming you are using Chrome (and you should be!) click on the three dot icon, “More Tools” and “Developer tools”.

The developer console (2) Soon, we will start thinking about the HTML and CSS that our JavaScript can manipulate…. But not today! Now we can see a place were JavaScript writes out content. The console! This text was generated by the JavaScript command: console.log(“hello world”);

Before we begin - a quick look at the code <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Bootstrap 4 References --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> <!-- Link to FontAwesome --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <title>Simple JavaScript Demo</title> <script> "use strict"; /* some simple examples based on the material covered in class */ console.log("hello world"); </script> </head> <body> <p>See the web developer console.</p> </body> </html> The Bootstrap and FontAwesome portions are standard for this class. You don’t need to memorize those!

Let’s zoom in… This is our one line of code <title>Simple JavaScript Demo</title> <script> "use strict"; /* some simple examples based on the material covered in class */ console.log("hello world"); </script> </head> <body> <p>See the web developer console.</p> </body> </html> JavaScript is to be written inside the <script> tag. The “use strict”; line is a best practice. We will talk about that more another day. Notice the characters used to indicate comments in JavaScript. /* comment goes here */ This is our one line of code

Now try this… DISCUSS <script> "use strict"; What did console.clear() do? What character does each JavaScript statement end with? <script> "use strict"; console.clear(); console.log("hello world"); console.log(10); </script> Data Types: JavaScript supports multiple data types. The two most intuitive data types are string and number. In the statement above what is string data? What is number data? How can you tell the difference?

Humble beginnings… Let’s add some more code to the bottom of the script tag… Another way to indicate a comment in JavaScript // comment goes here //write an expression to the console console.log(10 + 4); //make a variable var a=42; console.log(a); var b=19; console.log(b); Hey look! JavaScript can do math for us! This is called an expression. That is – we expressed a value (14) as the sum of 10 and 4. What we just did here is remarkable! Instead of the literal values 42 and 19, we put those values into something called a variable. The var statement means, make a new variable. The variables are: a and b. Variables are essential to make a programming language do anything useful!

Putting JavaScript to work (simple math) Here we combine a string with a variable. The result is a new string. //write expressions using variables. console.log("The value of a is: " + a); console.log("The value of b is: " + b); console.log(a + b); //addition (a + b) console.log(a - b); //subtraction (a - b) console.log(a * b); //multiplication (a * b) console.log(a / b); //division (a/b)

Order of precedence a = 10; b = 2; //order of precedence console.log(10 + a / b ); //implicit console.log(10 + (a / b) ); //explicit console.log( (10 + a) / b ); //explicit We already had variables a and b so we are not using var again here. We are just reassigning them. Discuss - One of these lines results in a different answer. Why?

Working with strings //how about some strings? console.log('Fred'); console.log("Velma"); //either single or double quotes is fine var name_one = 'Shaggy'; var name_two = 'Scooby'; console.log(name_one); console.log(name_two); //What happens when you try to add two strings together? // Think about it - a word like 'Scooby' does not have // a numeric value so ... console.log(name_one + name_two); We call this operation concatenation. The + sign does double duty in JavaScript. It represents both arithmetic addition as well as concatenation.

Now we’re cooking! // convert feet to inches var length_in_feet = 6; var length_in_inches = length_in_feet * 12; console.log(length_in_feet + " feet is equal to " + length_in_inches + " inches."); Notice that we used much more meaningful variable names in this example. We chose length_in_feet and length_in_inches instead of a and b. That makes our code easier to read!

Variables

Rules for naming variables Variable names are called identifiers. Identifiers can only contain letters, numbers, the underscore, and the dollar sign. Identifiers can’t start with a number. Identifiers are case-sensitive. Identifiers can be any length. Identifiers can’t be the same as reserved words. Avoid using global properties and methods as identifiers. So var and console would be poor variable names! Some examples: subtotal index_1 $ taxRate calculate_click $log

Camel casing versus underscore notation Camel case notation theCat theDog theHouse Underscore notation the_cat the_dog the_house Both are fine…

Naming recommendations for identifiers

More calculations // the area of a square var square_side_length = 123; var square_area = square_side_length * square_side_length; console.log('The area of a square with a side length of ' + square_side_length + ' is ' + square_area ); // the perimeter of a square var square_perimiter = square_side_length * 4; console.log('The perimeter of a square with a side length of ' + square_side_length + ' is ' + square_perimiter );

Fahrenheit to Celsius // convert Fahrenheit to Celsius // The formula is: (32°F − 32) × 5/9 = 0°C var farenhieght = 212; var celcius = (farenhieght - 32) * 5/9; console.log(farenhieght + " degrees farenhieght equals " + celcius + " degrees celcuis.");

Fancier stuff // fancier stuff - how long is the string? console.log("How long is the string: " + name_one); console.log(name_one.length); // fancier stuff - changing a string to integer var x = '1812'; console.log(parseInt(x)); var y = 'Elephant'; console.log(parseInt(y)); //fancier stuff - converting a number to string var pi = 3.14159265359; console.log(pi.toFixed(2));

On your own … Continue working in intro_js.html. Given a base and a height calculate the area of a triangle. Convert the answer to string and round the answer to two decimal places. Given a person's height in inches, and weight in pounds, compute the person's Body Mass Index. The BMI formula is: 703 x (weight in pounds) / (height squared) Convert the answer to a string, and round to one decimal place.