Presentation is loading. Please wait.

Presentation is loading. Please wait.

Javascript Intro Instructor: Shalen Malabon. Lesson Plan Core Javascript Syntax Types and Objects Functions DOM Event Handling Debugging Javascript Smarter.

Similar presentations


Presentation on theme: "Javascript Intro Instructor: Shalen Malabon. Lesson Plan Core Javascript Syntax Types and Objects Functions DOM Event Handling Debugging Javascript Smarter."— Presentation transcript:

1 Javascript Intro Instructor: Shalen Malabon

2 Lesson Plan Core Javascript Syntax Types and Objects Functions DOM Event Handling Debugging Javascript Smarter Form UI Enhancement Javascript Best Practices Advance Javascript Features

3 What you should know HTML and CSS

4 Javascript HTML- mark up language(content) CSS- style sheet language(presentation) Javascript – scripting language(behavior) Javascript is a language that's typically used inside websites, in order to manipulate the HTML output on the screen, pull in data from elsewhere, and do calculations.

5 JavaScript is limited Can’t access local files Can’t directly access the database Can’t access hardware(USB,etc)

6 Javascript is interpreted, not compiled. Javascript is case sensitive. Javasript is whitespace insensitive.

7 Javascript- is a client-side language

8 o JavaScript is *not* a "light" version of Java, but its own full- featured programming language, sharing some syntax with Java but also different in many ways

9 The Element

10 Statements -separate instructions or commands to say a piece of what you want or code will do. -terminated by a semi-colon

11 Activity 1 Simple Page Simple HTMLPage This is very simple HTML page It's about as basic as they come. It has : An h1 tag Two paragraphs An ordered list alert( "Hello World!" );

12 Where to put javascript? Position of the code matters

13 write() and writeln() methods: - Use to display text in a Web browser when the document is first rendered -the only difference between the write() and writeln() is that the writeln()method adds a line breaks after the line of text. Line breaks however are only recognized inside the element.

14 Case sensitivity in Javascript Like XHTML, JavaScript is case sensitive, and within javascript code, object names must always be all lower case. The statement: Document.write(“Welcome to Nashville”); Causes an error message because the JavaScript interpreter Cannot recognize an object named with an uppercase D.

15 Adding Comments Comments- are various types of remarks including the name of the program, your name and the date you created the program, notes to yourself or instructions to future programmers who may need to modify your work. Line comments: hides a single line of code. // This line comment takes up an entire line Block comments /* This line is a part of block comment. This line is also part of block comment. /*

16 Including element for each code section Multiple Script Sections First script section document.write(“ Output from the first script section ” ); Second script section document.write(“ Output from the second script section ” );

17 Logic and Debugging Logic – refers to the order in which various part of a program run or execute. Bug- any error in a program that causes it to function incorrectly, whether because of incorrect syntax or flaws in logic. Debugging – is the act of tracing and resolving errors in a program.

18 Using the Browser Console Javascript is usually evaluated after being included in a website, but modern browsers often come with a console that lets you run arbitrary JavaScript and see the results. The console can be used for quick experimentation, like for the upcoming concepts. FirefoxChrome Download the Firebug extension for Firefox.Go to Developer->Tools->Console or press CTRL+SHIFT+J.

19 Basic Variables Use var to declare a variable. Its initial value will be undefined. var x; It will work if you don't use var, but then it will become a global variable, which you usually don't want. You can later initialize it: x = 5; Or you can declare and initialize all at once: var x = 5;

20 var name; name name = “jasmine”; undefined jasmine

21 Basic Data Types Variables are loosely typed. They can be assigned to any data type, and can later be re-assigned to different data types. The primitive data types in Javascript are string, number, and boolean, undefined, and null: var x; x = "6"; x = 2 + 2; x = false; x = null; The complex data types are Arrays or Objects (covered later). Everything in JS is one of these types. **activity1

22 Numbers All numbers are 64 bit floating point - there's no differentiation between an int/float/double type. The standard operators for numbers work in Javascript: var x = 2 * 3; var y = x / 4; var z = x - y; When an operation doesn't result in a valid number, it returns NaN or in case of 1/0, Infinity. There are various utility functions in the Math library: var x = Math.abs(-454); var y = Math.pow(2, 3);...

23 Strings Strings are immutable, but a new string can be created easily with concatenation: var x = "hello"; var y = "world"; var z = x + " " + y; x += "!"; Single quotes and double quotes are the same: var x = "hi"; var x = 'hi'; Single quotes can be used inside double quotes, & vice versa: var x = "then he said, 'thats awesome!'"; var y = 'then he said, "thats awesome!"'; ** Activity 2

24 Strings Numbers Strings can be converted to numbers by doing simple math on them: var x = "43.232" * 1; Use parseFloat to turn a string into a number: var x = parseFloat("43.232"); Use parseInt to return a number with no digits after the decimal: var x = parseInt("43.232"); Numbers can be converted to strings with a formal cast, or just by appending an empty string. var x = 23 + ""; var x = String(23);

25 String Methods Strings have many native helper methods: var greeting = "Hello there"; greeting.charAt(0); // "H" - String indices are 0-based greeting.toUpperCase(); // "HELLO THERE" This indexOf function returns the start index of the first occurrence of a given character or substring, or -1 if not found. greeting.indexOf("e"); // 1 greeting.indexOf("there") // 6 greeting.indexOf("E") // -1 Method calls can be chained in Javascript: greeting.toUpperCase().indexOf("E") ** Activity 2

26 String Methods The slice method is a handy way to create substrings. slice(start, end) returns a copy of the string beginning at start and extending up to but not including end: 'Hello'.slice(1, 3); // "el" If no end is specified, it copies up to the end of the string: 'Hello'.slice(2); // "llo" The replace method is a handy way to create a new string by replacing a substring with another substring: 'hi, hi'.replace('hi', 'bye'); // "bye, hi"

27 Using in a Webpage JavaScript can be embedded inside script tags on a page: var x = "Hello World"; alert(x); Javascript can also be in external files, and referenced in a page:

28 Outputting to HTML The absolute simplest way to modify the HTML of a page using JavaScript is with the document.write method: document is a predefined browser variable that always points to the current HTML document. The write method will let us insert text (or HTML) into the document immediately following the element: document.write("Hello World"); Note:The inserted content will not appear in the browser's "view source" window (try it out), but it will appear in the page model accessible via JavaScript (more on that later).

29 Debugging with Browser Consoles The browser console will output any errors it encounters while running JS on the current page. The most common errors are trying to use functions or variables that don't exist. Try the following in an HTML file: alertt("alertt doesn't exist"); The console status bar will display "1 Error", and you can open up the console for more information. It will often point to the line of code that caused the error, and show a trace of the functions called before the error was called. You can also use console.log() to write out information to the console from within your web page's JS.

30 Arithmetic operators: + - / * Shorthand score= score + 10;score+=10; score= score - 10;score-=10; score= score / 10;score/=10; score= score * 10;score*=10;

31 Operator precedence result = 5+5 *10; Ans= 55

32 symbols ( ) parentheses [ ] brackets { }braces Operators: <!= >=== ==>= <=


Download ppt "Javascript Intro Instructor: Shalen Malabon. Lesson Plan Core Javascript Syntax Types and Objects Functions DOM Event Handling Debugging Javascript Smarter."

Similar presentations


Ads by Google