Lecture 9: JavaScript Syntax

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
JavaScript, Fourth Edition
JavaScript, Third Edition
C++ for Engineers and Scientists Third Edition
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
4. Python - Basic Operators
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
JavaScript, Fourth Edition
ICT Introduction to Programming Chapter 4 – Control Structures I.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Introduction to JavaScript CSc 2320 Fall 2014 Disclaimer: All words, pictures are adopted from “Simple JavaScript”by Kevin Yank and Cameron Adams and also.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CGS 3066: Web Programming and Design Spring 2017
Chapter 10 Programming Fundamentals with JavaScript
Chapter 6 JavaScript: Introduction to Scripting
Multiple variables can be created in one declaration
JavaScript Syntax and Semantics
User input We’ve seen how to use the standard output buffer
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 3: Understanding C# Language Fundamentals
Operators and Expressions
JavaScript and Ajax (Expression and Operators)
Arrays, For loop While loop Do while loop
Chapter 10 Programming Fundamentals with JavaScript
Arithmetic operations, decisions and looping
Java - Data Types, Variables, and Arrays
Chapter 8 JavaScript: Control Statements, Part 2
WEB PROGRAMMING JavaScript.
Module 2: Understanding C# Language Fundamentals
During the last lecture we had a discussion on Data Types, Variables & Operators
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Logical Operations In Matlab.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Chapter 2 Programming Basics.
Loops and Arrays in JavaScript
The Selection Structure
Using C++ Arithmetic Operators and Control Structures
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
CIS 136 Building Mobile Apps
Web Programming and Design
CGS 3066: Web Programming and Design Spring 2016
Review of Java Fundamentals
CGS 3066: Web Programming and Design Fall 2019
Presentation transcript:

Lecture 9: JavaScript Syntax Variables, Data Type, Statements

Variables Each variable has a name and value var firstname = “Guanyu”; var number = 1; Variable declaration. To declare a variable, use the var keyword. var age = 21; //so I can drink.

Variables Initialization: gives a variable its value for the first time. The value can change later, but it is only initialized once var lastname = “Tian”; Assignment: you can set a variable’s value many times. lastname = “Jordan”; Age = 50;

Variables What will happen if you use a variable that has never been declared or initialized?

Data Type JavaScript has dynamic types. The same variable can be used as different types: var x; //no data type var x = 5; //it is a number x = “Steven” //change to a string String is data type that stores a series of characters “google”, “apple”, “microsoft”

Data Type Numbers: JavaScript has only one type of numbers. Numbers can be written with, or without decimals var x = 34; var y = 34.00; Booleans: have two values: true or false var x = true; var y = false;s

Arithmetic Operations You can do many operations +, -, *, /, %. Precedence: if you have more than one operator, those operators are worked out in a particular order. 10 + 2 / 2 + 4 * 2 (10 + 2) / 2 + 4 * 2

Logic Operators Logic Operators works on booleans. They are used to compare two values, on the left and right of the operator, to produce a boolean value. Equality: use the double or triple equals operator 2 === 3, what is the result? 2 !== 3, what is the result? Strings containing a number and a number are not equal ‘10’ = ==10, what is the result?

Logic Operators Greater than (>) Less than (<) 10 > 5 Less than (<) 10 < 5 Combined comparisons 10 >= 10 10 <= 10

Logic Operators Greater than (>) Less than (<) 10 > 5 Less than (<) 10 < 5 Combined comparisons 10 >= 10 10 <= 10

Conditional Statement Logic is used to make decisions in code. You choose to run one piece of code depending on the logic comparison result. This logic comparison is called a conditional statement If statement: if some condition is true, run a block of code; otherwise, run another block of code.

Conditional Statement If statement: if ( 10 > 5 ){ //run the code here } If … else… statement: if( 10 > 5 ){ //run the code here } else{ //run a different piece of code here}

Loop Statement Loop is a way of repeating the same block of code over and over. While loop: repeats a block of code while a condition is true. var counter = 1; while(counter < 10){ alert(counter); counter++; //counter = counter + 1; }

For Statement For loop: it combines three semicolon-separated pieces information between the parentheses: initialization, condition and a final expression. for(var counter = 1; counter < 10; counter++) { alert(counter); }

Type Conversion Type conversion means to convert one type of variable into another type. There are explicit type conversion var num = 15 var n = num.toString(); // n is “15”

Type Conversion Type conversion means to convert one type of variable into another type. There are explicit type conversion var num = 15; var n = num.toString(); // n is “15” var test = true; Number(test);

Type Conversion Implicit conversion: automatically convert one type of variable to another type For *, -, /, %, >, <, each of these operator will automatically convert its operands to Number if they are not of this type. For example: window.alert(“5” – 2); //display 3

Type Conversion One exception is the + operator: if either of the operands of the + operator are of type String, then the non-String operand will automatically be converted to String, and then the two strings will be concatenated. For example, alert(“5” + 3); What is the result?

String Comparison If both operands to one of relational operators or of == and != are String, then lexicographic string comparison is performed. e.g: if(“fsu” == “fsu”) alert(“fsu”); if( “fsu” > “fau”) alert(“fau”);

Array An array is used to store multiple values in a single variable. An array is just one variable that contains a list of values. e.g., var numbers = new Array(); numbers[0] = 12; numbers[1] = 15; numbers[2] = 45; numbers[3] = 22; index value 12 15 1 2 45 3 22

Create and Access An Array var trees = new Array(“maple”, “ashley”, “oak”); var countries = [“America”, “Spain”, “China”]; Access an array: you refer to an element in an array using the index. var myCountry = countries[0];

Array Methods and Properties An array has predefined properties and methods: e.g: //the number of elements in an array named countries. alert(countries.length); //find the index of a particular element in an array. alert(countries.indexOf(“China”);