JavaScript Lecture 2

Similar documents
JavaScript Lecture 3b (Some language features)

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Client-Side Web Technologies. JavaScript Part I

Midterm Exam. 5. What is the character - (minus) used for in JavaScript? Give as many answers as you can.

COP4020 Programming Assignment 1 - Spring 2011

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

JavaScript Lecture 1

CGS 3066: Spring 2015 JavaScript Reference

Object interactions Lecture 6

CS313D: ADVANCED PROGRAMMING LANGUAGE

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware

CSC Web Programming. Introduction to JavaScript

Introduction to C# Applications

Topic 7: Algebraic Data Types

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

What is PHP? [1] Figure 1 [1]

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

Variables and Typing

Symbols. accessor properties, attributes, creating, adding properties, 8 anonymous functions, 20, 80

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

JavaScript I Language Basics

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Decaf Language Reference Manual

JavaScript. The Bad Parts. Patrick Behr

Introduce C# as Object Oriented programming language. Explain, tokens,

COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts

JavaScript: Coercion, Functions, Arrays

BASIC ELEMENTS OF A COMPUTER PROGRAM

COMP 202 Java in one week

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

Computer Science II (20082) Week 1: Review and Inheritance

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

PHP. Interactive Web Systems

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

Name Section: M/W or T/TH. True or False (14 Points)

JavaScript Syntax. Web Authoring and Design. Benjamin Kenwright

Sprite an animation manipulation language Language Reference Manual

CS 251 Intermediate Programming Methods and Classes

1 CS580W-01 Quiz 1 Solution

CS 251 Intermediate Programming Methods and More

Java Notes. 10th ICSE. Saravanan Ganesh

More sophisticated behaviour Lecture 09

INF5750. Introduction to JavaScript and Node.js

CSCE 120: Learning To Code

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Introduction to Java Applications

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

WYSE Academic Challenge Computer Science Test (State) 2013 Solution Set

HTML5 and CSS3 More JavaScript Page 1

Simple Java Reference

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Introduction. C provides two styles of flow control:

1. Variables, Again 2. JavaScript Operators Types 3. Iteration: the for-loop 4. Working with Strings 5. Making Decisions 6.

DaMPL. Language Reference Manual. Henrique Grando

Loops. In Example 1, we have a Person class, that counts the number of Person objects constructed.

CS1101: Lecture 9 The Shell as a Programming Language

CSCI 2101 Java Style Guide

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

COMP519 Web Programming Lecture 27: PHP (Part 3) Handouts

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Java Classes and Objects

LECTURE 02 INTRODUCTION TO C++

Full file at

Week 6 Lesson 1: Control-Flow Statements (Continued)

PHP 5 Introduction. What You Should Already Know. What is PHP? What is a PHP File? What Can PHP Do? Why PHP?

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

3. Java - Language Constructs I

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

TypeScript. Types. CS144: Web Applications

Project 3 Due October 21, 2015, 11:59:59pm

Functional JavaScript. Douglas Crockford Yahoo! Inc.

Programming for Experimental Research. Flow Control

Flow Control. CSC215 Lecture

COMP 202 Java in one week

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Name SECTION: 12:45 2:20. True or False (12 Points)

CSE 341 Section Handout #6 Cheat Sheet

CA4003 Compiler Construction Assignment Language Definition

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

Chapter 3 Data Types and Variables

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

This exam is open book. Each question is worth 3 points.

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

QueueBlock, ReversalADT, LinkedList,CustomerAccount, not MaintainCustomerData

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Introduction to R: Data Types

JavaScript: More Syntax

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

JavaScript: More Syntax and Using Events

Course Coding Standards

Transcription:

JavaScript Lecture 2 Waterford Institute of Technology May 5, 2016 John Fitzgerald Waterford Institute of Technology, JavaScriptLecture 2 1/28

JavaScript Introduction Topics discussed this presentation Identifiers Comments Reserved Words Operators Control Flow Truthy and Falsy Waterford Institute of Technology, JavaScriptLecture 2 2/28

Identifiers Style Guide Requirements May comprise only letters, numbers, $ sign, underscore Avoid single letter names. camelcase for objects, functions, instances. PascalCase for constructors, classes. Leading underscore _ for private properties. Number disallowed as first character. function q() {...} // bad function query {...} // good: descriptive const my_object = {} // bad const myobject = {} // good: camelcase const good = new User({...}); // good: PascalCase const $domelement = $(this).get(0); // good: jquery variable Waterford Institute of Technology, JavaScriptLecture 2 3/28

Reserved words Java Overlap Waterford Institute of Technology, JavaScriptLecture 2 4/28

Comments Single line comments Use // for single line comment. Position on new line above target of comment. If not start block, add blank line before comment. function getradius() { // return radius of circle.... } Waterford Institute of Technology, JavaScriptLecture 2 5/28

Comments Multi-line comments Use /**... */ for multi-line comments. Include description. Specify parameter types and values. Specify return type and value. / find() returns sought value based on parameter key. @param {String} key @return {Value} value. / function find(key) { //... return value; } Waterford Institute of Technology, JavaScriptLecture 2 6/28

Operators Assignment const x = 5; const y = 2; let z = x + y; z = 2; // => 18 z /= 3; // => 6 x % 3; // => 2 Waterford Institute of Technology, JavaScriptLecture 2 7/28

Operators Arithmetic const x = 5; const y = 2; const z = x y; // => 10 z ; // => 9 Waterford Institute of Technology, JavaScriptLecture 2 8/28

Operators String let txt1 = 'What a very '; txt1 += 'nice day.'; // Output What a very nice day. Waterford Institute of Technology, JavaScriptLecture 2 9/28

Operators Add Number to String const x = 5 + 5; // => 10 const y = '5' + 5; // => 55 const z = 'Hello' + 5; // => Hello5 Waterford Institute of Technology, JavaScriptLecture 2 10/28

Operators Comparison & Logical const s = '5'; const n = 5; s == n // => true (coercion) s === n // => false (strict) Waterford Institute of Technology, JavaScriptLecture 2 11/28

Operators Type const car = 'Nissan'; typeof car; // => string const cars = ['Saab', 'Volvo', 'BMW']; cars instanceof Array; // => true Waterford Institute of Technology, JavaScriptLecture 2 12/28

Operators Equals and not equals // Use always ===!== // Evil twin (Crockford) ==!= Waterford Institute of Technology, JavaScriptLecture 2 13/28

Control Flow Truthy and Falsy Expression either truthy or falsy. Some developers avoid use. Not reserved words. Waterford Institute of Technology, JavaScriptLecture 2 14/28

Control Flow Truthy and Falsy Falsy false null undefined Empty string Number 0 NaN Truthy All values not truthy Warning: string false is truthy Waterford Institute of Technology, JavaScriptLecture 2 15/28

Control Flow Truthy and Falsy Function to determine if value falsy / Determines if argument resolves to a falsy or truthy value. @see https://developer.mozilla.org/en US/docs/Web/JavaScript/Reference @param arg Argument to be checked if falsy. @return Returns true if argument is falsy, otherwise false. / function falsy(arg) { return [false, null, undefined, '', 0, NaN].includes(arg); } Waterford Institute of Technology, JavaScriptLecture 2 16/28

Control Flow Logical Operators (AND, OR) Significantly different behaviour to Java x && y // => x if x falsy otherwise y x y // => x if x truthy otherwise y // Use OR to insert default values const person = {}; let name = person.name 'No such person'; console.log(name); // => No such person person.name = 'Jane Doe'; name = person.name 'no such person'; console.log(name); // => Jane Doe Waterford Institute of Technology, JavaScriptLecture 2 17/28

Control Flow Logical Operators (AND, OR) Significantly different behaviour to Java x && y // => x if x falsy otherwise y x y // => x if x truthy otherwise y // Use AND to avoid TypeError exception const flight = {}; flight.airline; // => undefined flight.airline.nationality; // => TypeError flight.airline && flight.airline.nationality; // undefined Waterford Institute of Technology, JavaScriptLecture 2 18/28

Control Flow Loop using for const limit = 5; for (let i = 0; i < limit; i++) { text += 'The number is ' + i + '<br>'; } Waterford Institute of Technology, JavaScriptLecture 2 19/28

Control Flow Using while let i = 0; while (i < 10) { text += 'The number is ' + i; i++; } Waterford Institute of Technology, JavaScriptLecture 2 20/28

Control Flow Using do-while let i = 0; const limit = 10; do { text += 'The number is ' + i; i++; } while (i < limit); Waterford Institute of Technology, JavaScriptLecture 2 21/28

Control Flow Using if-else if (hour < 18) { greeting = 'Good day'; } else { greeting = 'Good evening'; } Waterford Institute of Technology, JavaScriptLecture 2 22/28

Control Flow Using break statement break statement Checks for condition If met then immediately exits loop // This trivial function return 0 function foo() { const limit = 10; const sum = 0; for (let i = 0; i < limit; i += 1) { if (i % 2 == 0) break; else sum += i; } return sum; }; Waterford Institute of Technology, JavaScriptLecture 2 23/28

Control Flow Using continue statement continue statement Checks for condition If met, skips remainder iteration Continues with next iteration // Calculate sum of odd integers in range [0, 10] function addodd() { const limit = 10; let sum = 0; for (let i = 0; i < limit; i += 1) { if (i % 2 == 0) continue; else sum += i; } return sum; }; Waterford Institute of Technology, JavaScriptLecture 2 24/28

Control Flow Ternary (Conditional) Operator // If argument a is negative return a. Otherwise, return a function absolutevalue(a) { return a < 0? a : a; }; Waterford Institute of Technology, JavaScriptLecture 2 25/28

Control Flow Logical Operators function foo() { const limit = 5; let i = 0; let j = 0; let sum = 0; while (++i < limit && ++j < limit) { sum += i + j; } return sum; }; console.log(foo()); // 20 Waterford Institute of Technology, JavaScriptLecture 2 26/28

Control Flow Using switch statement switch (new Date().getDay()) { case 0: case 6: day = 'Weekend'; break; default: day = 'Weekday'; } Waterford Institute of Technology, JavaScriptLecture 2 27/28

Waterford Institute of Technology, JavaScriptLecture 2 28/28