Variables and Typing

Size: px
Start display at page:

Download "Variables and Typing"

Transcription

1 Variables and Typing Christopher M. Harden Contents 1 The basic workflow 2 2 Variables Declaring a variable Assigning to a variable Other types of variables Type system Types Strings Numbers Coercion Try it yourself 9 1

2 1 The basic workflow JavaScript code is written in a script file. The bare minimum required to write a script is a plain text editor. There are a variety of programs you can use to write JavaScript script files, with useful functions such as syntax highlighting and code validation. In your own time you can look at various programs and see what works best for you. In classes, I will always use Notepad, which exists on every Windows installation. Linux users, with easy access to a terminal, should also find a plain text editor as standard on their distro (though each distro may pick a different editor, they all have the same function). Note. Programs such as Microsoft Word will not produce plain text. Such documents will contain hidden markup to represent properties of the document, and a JavaScript interpreter will see that markup and get quickly confused. Since our code will eventually end up on a website, what we will do is create a basic HTML page. When we load the page in a web browser such as Firefox or Chrome, the script will run. To do this, we put listing 1 into a text file called test.html, and then open the file in a web browser with Ctrl-O. Simply replace script.js with whatever your file is called, and place this in the same directory as your script. Listing 1: HTML Boilerplate Code 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <t i t l e>javascript Scratchpad</ t i t l e> 5 <script src="script.js"></ script> 6 </head> 7 <body> 8 </body> 9 </html> Note. Modern web browsers tend to have scratchpads built in if you wish to use them. Firefox users will find one in developer options, aptly called Scratchpad. Chrome/Chromium users will find in the developer console a tab labelled Sources. Right click in the blank area under the Snippits tab to create a new file to type in. Once you have a workflow you prefer, we must test it. script.js can be found in listing 2. 2 An example

3 1 "use strict" ; Listing 2: Your first JavaScript script 2 3 // This should create a box onscreen 4 a l e r t ( "Hello world!" ) ; JavaScript files contain a list of commands separated by semicolons. If you forget the semicolons, the JavaScript interpreter (the program responsible for running your code) will attempt to put them in for you. It is best that you always put them in yourself, to remove the possibility of the interpreter making a mistake. Line 1 is a special command which activates strict mode. Strict mode is recommended to be used, and should appear at the top of every script. This line is magic, and can only appear in the first line. For now, just place it at the top of scripts you write. Changes will be marked as the class progresses. Line 3 is called a comment. Every on a line after // is ignored by the interpreter. Thus you may use them to describe what is happening in your program for future reference. Well written comments will help you understand code better, particularly if you wrote it many years previously! For wordier comments, you can surround text with /* and */. This can take up multiple lines if you choose, and can contain absolutely anything. Line 4 is the main part. It requests a box be displayed with the text Hello world! in it without quotes. 2 Variables The basic concept of any programming language is data. Almost every script or program will have to store and manipulate data in some form. To do this, we use variables. 2.1 Declaring a variable Before we can use a variable, we must declare that it exists and what it is called. You can name a variable using letters, numbers, or the symbols and $. There are only two restrictions. You cannot start a variable name with a number, and you cannot use any of the words listed below. break catch const debugger case class continue default 3

4 delete if private try do else enum implements import in protected public return typeof var export instanceof static void extends finally for interface let new super switch this while with function package throw yield Once you have a name for a variable, declare it with the var keyword. For example, to declare a variable called firstprimeministerofuk, we write 1 var firstprimeministerofuk ; Listing 3: Declaring a variable Note. Remember that firstprimeministerofuk, firstprimeministerofuk, and firstprimeministerofuk are all different names. Case matters! Once declared, a variable exists for as long as it is in scope. The true meaning of this statement will not be apparent until later classes. For now, just remember that variables declared with var can be seen by any script on the same web page at any time. Officially, we say that variables declared with var have global scope. 2.2 Assigning to a variable Once we have a variable, we can use it to store data. For example, the first Prime Minister of the United Kingdom is generally considered to be Sir Robert Walpole. To store this information in the variable we declared earlier we write Listing 4: Assigning to a variable 1 firstprimeministerofuk = "Sir Robert Walpole" ; If we know what to assign to a variable the moment we declare it, we can roll these two statements into one line. This is called initialization, and it is 4

5 recommended to initialize variables whenever possible. Thus we can replace the last two statements with listing 5. Listing 5: Initializing a variable 1 var firstprimeministerofuk = "Sir Robert Walpole" ; Variables can be reassigned whenever you like. With our variable above, while Sir Robert Walpole is considered by historians to be the first Prime Minister, he himself denied such a job even existed. The first mention of a Prime Minister in legal documents is in a treaty signed by Benjamin Disraeli. To change our variable to reflect this, we write Listing 6: Reassigning to a variable 1 firstprimeministerofuk = "Benjamin Disraeli" ; Once a variable has been assigned to, its former contents are gone forever and cannot be recovered. If you assign to a variable you haven t declared yet, the results depend on if you opted into strict mode or not. If you have not, JavaScript will helpfully declare it immediately on your behalf (as a var). If you have then JavaScript will report an error and stop immediately. To avoid mistakes such as accidental misspellings of variable names it is recommended to use strict mode. 2.3 Other types of variables There are two other methods of declaring a variable. Both of these methods have block scope, which will be important to remember for when we meet blocks. The first new keyword is let. It is used in the exact same way as var, and apart from the concept of block scope, is exactly the same. It is recommended to use let whenever possible, and var only when necessary. For example 1 let c a l o r i e s I n C a k e = 5118; Listing 7: Declaring a variable with let The final method of declaring a variable is const. const declares a constant. In the context of a JavaScript script, a constant is a variable that you promise not to reassign. At the time of writing this document, Barack Obama is the current President of the USA. Unlike our Prime Minister example above, there is no question on who is the first President of the USA. Thus we can count all the Presidents and find that Barack is the 44th. No 5

6 matter what happens in the future, the 44th President will never change. A constant would be best for representing this fact, which we do in listing 8. Listing 8: Declaring a variable with const 1 const PresidentofUSANo44 = "Barack Obama" ; 2 // This next line will generate an error! 3 PresidentofUSANo44 = "John McCain" ; As promised, if you attempt to run listing 8, an error will indeed occur the moment you attempt to reassign this variable. We promised not to do so in our script, and the JavaScript interpreter will enforce that restriction for as long as this variable exists. Note how the constant must be initialized, since we can t assign to it later. Constants do not just represent unchanging facts, but merely variables we promise not to change. For example, if we have the base and height of a triangle in aptly named variables, it is valid to write Listing 9: Constants as a promise not to reassign 1 const areaoftriangle = base height / 2 ; Even though the area changes depending on the base and height, once we know what triangle we are looking at we know what its area is. As long as we do not need to change the base or height of the triangle, we never need to change its area. It is recommended to use constants whenever possible. 3 Type system 3.1 Types Since computers store data as binary numbers, there is a possibility of confusion on what a variable contains. For example, the letter a is stored in a computer using the ASCII encoding. Under ASCII, a is stored as the number 97, which could be confused for the actual number 97. To handle this problem, JavaScript uses the concept of typing. A variable has a type attached to it, so that when you store the letter a you can retrieve it later. This type changes as needed automatically. There are 6 primitive data types. These are called strings, numbers, booleans, undefined, null, and Symbols. Additionally, there is one nonprimitive data type called Object. If you ever need to know what type your variable is, you can use the typeof keyword as in listing 10. The result is a string representing that type. 6

7 1 var a = "Hello" ; 2 typeof a ; // "string" 3 a = 2 1 ; 4 typeof a ; // "number" 5 a = "Goodbye" ; 6 typeof a ; // "string" Listing 10: Checking the type of a variable Strings A character represents an individual letter, number, space, punctuation, symbols, and so on. Anything you can consider text is a character. A sequence of these characters is called a string. Strings can be used to represent almost anything, and as such is one of the most common types you will see. To type a string into JavaScript, you must quote them with either or. There is no difference in using either quote, as long as you use the same quote on each end of the string. However, the choice is useful in trying to store strings that themselves contain either a or. If you need to store a string with both of these characters in them, you must escape the quote that needs to be in the string. To do that, prefix the quote with the \ character. If your string needs an actual \ character, you can escape the \ with another \. Note. The \ also allows you to type in specific characters that are otherwise hard to type manually. This is called escape notation. One such example is the newline character, represented by \n. In JavaScript these characters are not used often. However, it is useful to note both in case they are needed and if you accidentally use one. 1 "Hello" ; Listing 11: Example strings 2 "Hello", she greeted. 3 "Hello", David\ s sister greeted. 4 "Hello", David\\\ s sister greeted. The most common operation performed on strings is called concatenation. This glues together two strings to make one larger string, and is performed with the + operator. An example can be seen in listing 12. Listing 12: String concatenation 7

8 1 const g r e e t i n g = "Hello" ; 2 var t a r g e t = world ; 3 4 a l e r t ( g r e e t i n g + " " + t a r g e t + "!" ) ; Numbers The number type represents general numbers. More specifically, it represents either a decimal number or a few numerical concepts such as infinity. A number may have an optional + or - denoting its sign, and it may use a period as a decimal separator. This type is for performing calculations with numbers, and not for representing items such as fax numbers or addresses. Note. The use of a comma to separate numbers, such as 1, 234 is not supported. Additionally, a period is the only decimal separator, so one-half is written as 0.5 and not 0, 5. Regardless of your native language, JavaScript is written as if you was an American English speaker. Just like with regular numbers, you can add, subtract, multiply, and divide with +, -, *, and / respectively. These operators match the standard arithmetic rules, namely that * and / are prioritised over + and -, and that these operators are associative when appropriate. New in the current standard is the exponentiation operator, represented by **. Exponentiation also follows standard exponentiation rules, such as being approximately equal to , and not If you are targeting web browsers that do not support this, there is an alternative using Math.pow. Note that a b is equivalent to Math.pow(a, b). The dot in Math.pow has special meaning we will discuss later, and is not a part of the name. If you need to deviate from the standard rules, or if you wish to make a complicated expression easier to look at, you may use ( and ) to group parts of an expression together. As in regular mathematics, brackets are top priority and will be evaluated first. An example of everything discussed can be seen in listing 13. Listing 13: Working with numbers 1 const pi = ; 2 var r a d i u s = ( ) / 2 ; 3 4 const area = pi r 2; 5 // This is the Math.pow equivalent 8

9 6 const area = pi Math. pow( r, 2) ; 7 8 a l e r t ( "The circle of radius " + r a d i u s + " has area " + area ) ; 3.2 Coercion The + operator can be used to concatenate two strings and add two numbers. As long as the types match the JavaScript interpreter can work out what to do. However, if the types don t match then the interpreter cannot proceed. To fix this problem, JavaScript employs a technique called type coercion. For the + operator, if a number and a string is found then the number is turned into a string first. This is how the alert in listing 13 worked. To avoid having to remember type coercion rules for every operator, you can do the conversion yourself. Listing 14: Converting between types 1 var iamastring = S t r i n g (12) ; 2 var iamanumber = Number( "12" ) ; 3 4 typeof iamastring ; // "string" 5 typeof iamanumber ; // "number" This is also useful to know for dealing with functions (a topic to be discussed later). For example, prompt("what is your age?") will create a popup box containing a text field. The user can type an answer to your question in that box, and the function will return it. However, any answer given will always be returned as a string. Attempting to add to this number will cause concatenation to occur. This problem can be solved by converting the string into a number before using it. 4 Try it yourself To check this week s knowledge, try the following tasks below. Each task should take no more than a few minutes, and does not require information from a future class. 1. Try saving and running the code in listings 1 and 2. 9

10 2. Create appropriately named variables that stores the name, address, phone number, and number of years of operation for a fictitious company. Explain, using comments, what type each variable is, and why it is appropriate for that variable. 3. The command alert ("text") will display text to the user in an alert box. The command prompt("question") will display a question to the user, and this answer can be saved to a variable using 1 var answer = prompt ( " question" ) ; Assuming the user does not cancel, it will be a string containing the answer. Using these commands: (a) Get the name of the user, and display Hello name, where name is the name you was given by the user. (b) Get two numbers from the user, and display their sum. 10

CSC Web Programming. Introduction to JavaScript

CSC Web Programming. Introduction to JavaScript CSC 242 - Web Programming Introduction to JavaScript JavaScript JavaScript is a client-side scripting language the code is executed by the web browser JavaScript is an embedded language it relies on its

More information

CSCE 120: Learning To Code

CSCE 120: Learning To Code CSCE 120: Learning To Code Manipulating Data I Introduction This module is designed to get you started working with data by understanding and using variables and data types in JavaScript. It will also

More information

Chapter 17. Fundamental Concepts Expressed in JavaScript

Chapter 17. Fundamental Concepts Expressed in JavaScript Chapter 17 Fundamental Concepts Expressed in JavaScript Learning Objectives Tell the difference between name, value, and variable List three basic data types and the rules for specifying them in a program

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Web Site Design and Development JavaScript

Web Site Design and Development JavaScript Web Site Design and Development JavaScript CS 0134 Fall 2018 Tues and Thurs 1:00 2:15PM JavaScript JavaScript is a programming language that was designed to run in your web browser. 2 Some Definitions

More information

3 The Building Blocks: Data Types, Literals, and Variables

3 The Building Blocks: Data Types, Literals, and Variables chapter 3 The Building Blocks: Data Types, Literals, and Variables 3.1 Data Types A program can do many things, including calculations, sorting names, preparing phone lists, displaying images, validating

More information

Variables and Constants

Variables and Constants 87 Chapter 5 Variables and Constants 5.1 Storing Information in the Computer 5.2 Declaring Variables 5.3 Inputting Character Strings 5.4 Mistakes in Programs 5.5 Inputting Numbers 5.6 Inputting Real Numbers

More information

Program Design Phase. Algorithm Design - Mathematical. Algorithm Design - Sequence. Verify Algorithm Y = MX + B

Program Design Phase. Algorithm Design - Mathematical. Algorithm Design - Sequence. Verify Algorithm Y = MX + B Program Design Phase Write Program Specifications Analysis of requirements Program specifications description Describe what the goals of the program Describe appearance of input and output Algorithm Design

More information

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

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1) Technology & Information Management Instructor: Michael Kremer, Ph.D. Class 1 Professional Program: Data Administration and Management JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1) WHO

More information

2 nd Week Lecture Notes

2 nd Week Lecture Notes 2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous

More information

Working with JavaScript

Working with JavaScript Working with JavaScript Creating a Programmable Web Page for North Pole Novelties 1 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page 2 Objectives

More information

CGS 3066: Spring 2015 JavaScript Reference

CGS 3066: Spring 2015 JavaScript Reference CGS 3066: Spring 2015 JavaScript Reference Can also be used as a study guide. Only covers topics discussed in class. 1 Introduction JavaScript is a scripting language produced by Netscape for use within

More information

2 rd class Department of Programming. OOP with Java Programming

2 rd class Department of Programming. OOP with Java Programming 1. Structured Programming and Object-Oriented Programming During the 1970s and into the 80s, the primary software engineering methodology was structured programming. The structured programming approach

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

BASIC ELEMENTS OF A COMPUTER PROGRAM

BASIC ELEMENTS OF A COMPUTER PROGRAM BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators

More information

Introduction to C# Applications

Introduction to C# Applications 1 2 3 Introduction to C# Applications OBJECTIVES To write simple C# applications To write statements that input and output data to the screen. To declare and use data of various types. To write decision-making

More information

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are: LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words

More information

JME Language Reference Manual

JME Language Reference Manual JME Language Reference Manual 1 Introduction JME (pronounced jay+me) is a lightweight language that allows programmers to easily perform statistic computations on tabular data as part of data analysis.

More information

People = End Users & Programmers. The Web Browser Application. Program Design Phase. Algorithm Design -Mathematical Y = MX + B

People = End Users & Programmers. The Web Browser Application. Program Design Phase. Algorithm Design -Mathematical Y = MX + B The Web Browser Application People = End Users & Programmers Clients and Components Input from mouse and keyboard Controller HTTP Client FTP Client TCP/IP Network Interface HTML/XHTML CSS JavaScript Flash

More information

JScript Reference. Contents

JScript Reference. Contents JScript Reference Contents Exploring the JScript Language JScript Example Altium Designer and Borland Delphi Run Time Libraries Server Processes JScript Source Files PRJSCR, JS and DFM files About JScript

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

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

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1) Technology & Information Management Instructor: Michael Kremer, Ph.D. Class 2 Professional Program: Data Administration and Management JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1) AGENDA

More information

Client-Side Web Technologies. JavaScript Part I

Client-Side Web Technologies. JavaScript Part I Client-Side Web Technologies JavaScript Part I JavaScript First appeared in 1996 in Netscape Navigator Main purpose was to handle input validation that was currently being done server-side Now a powerful

More information

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA 1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley PHP and MySQL for Dynamic Web Sites Intro Ed Crowley Class Preparation If you haven t already, download the sample scripts from: http://www.larryullman.com/books/phpand-mysql-for-dynamic-web-sitesvisual-quickpro-guide-4thedition/#downloads

More information

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

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies Internet t Software Technologies JavaScript part one IMCNE A.A. 2008/09 Gabriele Cecchetti Why introduce JavaScript To add dynamicity and interactivity to HTML pages 2 What s a script It s a little interpreted

More information

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. Today! Build HelloWorld yourself in BlueJ and Eclipse. Look at all the Java keywords. Primitive Types. HelloWorld in BlueJ 1. Find BlueJ in the start menu, but start the Select VM program instead (you

More information

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

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION EDIABAS Electronic Diagnostic Basic System BEST/2 LANGUAGE DESCRIPTION VERSION 6b Copyright BMW AG, created by Softing AG BEST2SPC.DOC CONTENTS CONTENTS...2 1. INTRODUCTION TO BEST/2...5 2. TEXT CONVENTIONS...6

More information

A First Program - Greeting.cpp

A First Program - Greeting.cpp C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

JavaScript Basics. The Big Picture

JavaScript Basics. The Big Picture JavaScript Basics At this point, you should have reached a certain comfort level with typing and running JavaScript code assuming, of course, that someone has already written it for you This handout aims

More information

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

What is PHP? [1] Figure 1 [1] PHP What is PHP? [1] PHP is an acronym for "PHP: Hypertext Preprocessor" PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP is free to download and use Figure

More information

C++ Support Classes (Data and Variables)

C++ Support Classes (Data and Variables) C++ Support Classes (Data and Variables) School of Mathematics 2018 Today s lecture Topics: Computers and Programs; Syntax and Structure of a Program; Data and Variables; Aims: Understand the idea of programming

More information

Programming Lecture 3

Programming Lecture 3 Programming Lecture 3 Expressions (Chapter 3) Primitive types Aside: Context Free Grammars Constants, variables Identifiers Variable declarations Arithmetic expressions Operator precedence Assignment statements

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Functions and Objects

Functions and Objects Functions and Objects Christopher M. Harden Contents 1 Arrays 2 1.1 Defining arrays.......................... 2 1.2 Array length............................ 3 1.3 Multi-dimensional arrays.....................

More information

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Java Programming Fundamentals - Day Instructor: Jason Yoon Website: Java Programming Fundamentals - Day 1 07.09.2016 Instructor: Jason Yoon Website: http://mryoon.weebly.com Quick Advice Before We Get Started Java is not the same as javascript! Don t get them confused

More information

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications JavaScript Started as a simple script in a Web page that is interpreted and run by the browser Supported by most modern browsers Allows dynamic update of a web page More generally, allows running an arbitrary

More information

CMPT 100 : INTRODUCTION TO

CMPT 100 : INTRODUCTION TO CMPT 100 : INTRODUCTION TO COMPUTING TUTORIAL #5 : JAVASCRIPT 2 GUESSING GAME 1 By Wendy Sharpe BEFORE WE GET STARTED... If you have not been to the first tutorial introduction JavaScript then you must

More information

COMS 469: Interactive Media II

COMS 469: Interactive Media II COMS 469: Interactive Media II Agenda Review Data Types & Variables Decisions, Loops, and Functions Review gunkelweb.com/coms469 Review Basic Terminology Computer Languages Interpreted vs. Compiled Client

More information

C# Programming Tutorial Lesson 1: Introduction to Programming

C# Programming Tutorial Lesson 1: Introduction to Programming C# Programming Tutorial Lesson 1: Introduction to Programming About this tutorial This tutorial will teach you the basics of programming and the basics of the C# programming language. If you are an absolute

More information

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

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos. Lecture 2: Variables and Operators AITI Nigeria Summer 2012 University of Lagos. Agenda Variables Types Naming Assignment Data Types Type casting Operators Declaring Variables in Java type name; Variables

More information

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */ Overview Language Basics This chapter describes the basic elements of Rexx. It discusses the simple components that make up the language. These include script structure, elements of the language, operators,

More information

This tutorial will teach you about operators. Operators are symbols that are used to represent an actions used in programming.

This tutorial will teach you about operators. Operators are symbols that are used to represent an actions used in programming. OPERATORS This tutorial will teach you about operators. s are symbols that are used to represent an actions used in programming. Here is the link to the tutorial on TouchDevelop: http://tdev.ly/qwausldq

More information

Chapter 2 Working with Data Types and Operators

Chapter 2 Working with Data Types and Operators JavaScript, Fourth Edition 2-1 Chapter 2 Working with Data Types and Operators At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics

More information

JavaScript: Introductionto Scripting

JavaScript: Introductionto Scripting 6 Comment is free, but facts are sacred. C. P. Scott The creditor hath a better memory than the debtor. James Howell When faced with a decision, I always ask, What would be the most fun? Peggy Walker Equality,

More information

Web Scripting using PHP

Web Scripting using PHP Web Scripting using PHP Server side scripting No Scripting example - how it works... User on a machine somewhere Server machine So what is a Server Side Scripting Language? Programming language code embedded

More information

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

Using EnScript to Make Your Life Easier Session 1. Suzanne Widup, James Habben, Bill Taroli

Using EnScript to Make Your Life Easier Session 1. Suzanne Widup, James Habben, Bill Taroli Using EnScript to Make Your Life Easier Session 1 Suzanne Widup, James Habben, Bill Taroli 1 Master Title Session 1 Getting Started with EnScript 2 EnScript Basics To Begin With EnScript is similar to

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

Sprite an animation manipulation language Language Reference Manual

Sprite an animation manipulation language Language Reference Manual Sprite an animation manipulation language Language Reference Manual Team Leader Dave Smith Team Members Dan Benamy John Morales Monica Ranadive Table of Contents A. Introduction...3 B. Lexical Conventions...3

More information

COMP519 Practical 5 JavaScript (1)

COMP519 Practical 5 JavaScript (1) COMP519 Practical 5 JavaScript (1) Introduction This worksheet contains exercises that are intended to familiarise you with JavaScript Programming. While you work through the tasks below compare your results

More information

CHAD Language Reference Manual

CHAD Language Reference Manual CHAD Language Reference Manual INTRODUCTION The CHAD programming language is a limited purpose programming language designed to allow teachers and students to quickly code algorithms involving arrays,

More information

An overview of Java, Data types and variables

An overview of Java, Data types and variables An overview of Java, Data types and variables Lecture 2 from (UNIT IV) Prepared by Mrs. K.M. Sanghavi 1 2 Hello World // HelloWorld.java: Hello World program import java.lang.*; class HelloWorld { public

More information

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications JavaScript Started as a simple script in a Web page that is interpreted and run by the browser Supported by most modern browsers Allows dynamic update of a web page More generally, allows running an arbitrary

More information

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object CS1046 Lab 5 Timing: This lab should take you approximately 2 hours. Objectives: By the end of this lab you should be able to: Recognize a Boolean variable and identify the two values it can take Calculate

More information

What is XHTML? XHTML is the language used to create and organize a web page:

What is XHTML? XHTML is the language used to create and organize a web page: XHTML Basics What is XHTML? XHTML is the language used to create and organize a web page: XHTML is newer than, but built upon, the original HTML (HyperText Markup Language) platform. XHTML has stricter

More information

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective- UNIT -II Style Sheets: CSS-Introduction to Cascading Style Sheets-Features- Core Syntax-Style Sheets and HTML Style Rle Cascading and Inheritance-Text Properties-Box Model Normal Flow Box Layout- Beyond

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

COMP 202 Java in one week

COMP 202 Java in one week COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

CS1046 Lab 4. Timing: This lab should take you 85 to 130 minutes. Objectives: By the end of this lab you should be able to:

CS1046 Lab 4. Timing: This lab should take you 85 to 130 minutes. Objectives: By the end of this lab you should be able to: CS1046 Lab 4 Timing: This lab should take you 85 to 130 minutes. Objectives: By the end of this lab you should be able to: Define the terms: function, calling and user-defined function and predefined function

More information

Web Scripting using PHP

Web Scripting using PHP Web Scripting using PHP Server side scripting So what is a Server Side Scripting Language? Programming language code embedded into a web page PERL PHP PYTHON ASP Different ways of scripting the Web Programming

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Typescript on LLVM Language Reference Manual

Typescript on LLVM Language Reference Manual Typescript on LLVM Language Reference Manual Ratheet Pandya UNI: rp2707 COMS 4115 H01 (CVN) 1. Introduction 2. Lexical Conventions 2.1 Tokens 2.2 Comments 2.3 Identifiers 2.4 Reserved Keywords 2.5 String

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual Contents 1 Introduction...2 2 Lexical Conventions...2 3 Types...3 4 Syntax...3 5 Expressions...4 6 Declarations...8 7 Statements...9

More information

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018 Welcome to CSE 142! Whitaker Brand University of Washington, Winter 2018 1 What is computer science? computers? science? programming? late lonely nights in front of the computer? ALGORITHMIC THINKING al

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Programming for Engineers Introduction to C

Programming for Engineers Introduction to C Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

More information

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

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

Programming language components

Programming language components Programming language components syntax: grammar rules for defining legal statements what's grammatically legal? how are things built up from smaller things? semantics: what things mean what do they compute?

More information

Accelerating Information Technology Innovation

Accelerating Information Technology Innovation Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lesson 02 Variables and Operators Agenda Variables Types Naming Assignment Data Types Type casting Operators

More information

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA Starting with a great calculator... Topic 5: Introduction to Programming in Matlab CSSE, UWA! MATLAB is a high level language that allows you to perform calculations on numbers, or arrays of numbers, in

More information

CITS2401 Computer Analysis & Visualisation

CITS2401 Computer Analysis & Visualisation FACULTY OF ENGINEERING, COMPUTING AND MATHEMATICS CITS2401 Computer Analysis & Visualisation SCHOOL OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING Topic 3 Introduction to Matlab Material from MATLAB for

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 10 Exercise

More information

Part III Appendices 165

Part III Appendices 165 Part III Appendices 165 Appendix A Technical Instructions Learning Outcomes This material will help you learn how to use the software you need to do your work in this course. You won t be tested on it.

More information

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. Lesson 2 VARIABLES Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. WHAT ARE VARIABLES? When you input data (i.e. information) into a computer

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

Language Reference Manual simplicity

Language Reference Manual simplicity Language Reference Manual simplicity Course: COMS S4115 Professor: Dr. Stephen Edwards TA: Graham Gobieski Date: July 20, 2016 Group members Rui Gu rg2970 Adam Hadar anh2130 Zachary Moffitt znm2104 Suzanna

More information

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output Last revised January 12, 2006 Objectives: 1. To introduce arithmetic operators and expressions 2. To introduce variables

More information

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab. University of Technology Laser & Optoelectronics Engineering Department C++ Lab. Second week Variables Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable.

More information

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder Overview: Programming Concepts Programming: Act of formulating

More information

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder Overview: Programming Concepts Programming: Act of formulating

More information

9. Elementary Algebraic and Transcendental Scalar Functions

9. Elementary Algebraic and Transcendental Scalar Functions Scalar Functions Summary. Introduction 2. Constants 2a. Numeric Constants 2b. Character Constants 2c. Symbol Constants 2d. Nested Constants 3. Scalar Functions 4. Arithmetic Scalar Functions 5. Operators

More information

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead. Chapter 9: Rules Chapter 1:Style and Program Organization Rule 1-1: Organize programs for readability, just as you would expect an author to organize a book. Rule 1-2: Divide each module up into a public

More information

Programming Practice (vs Principles)

Programming Practice (vs Principles) Programming Practice (vs Principles) Programming in any language involves syntax (with rules, parentheses, etc) as well as format (layout, spacing, style, etc). Syntax is usually very strict, and any small

More information

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu) I. INTERACTIVE MODE VERSUS SCRIPT MODE There are two ways to use the python interpreter: interactive mode and script mode. 1. Interactive Mode (a) open a terminal shell (terminal emulator in Applications

More information

The Warhol Language Reference Manual

The Warhol Language Reference Manual The Warhol Language Reference Manual Martina Atabong maa2247 Charvinia Neblett cdn2118 Samuel Nnodim son2105 Catherine Wes ciw2109 Sarina Xie sx2166 Introduction Warhol is a functional and imperative programming

More information

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications JavaScript CS 4640 Programming Languages for Web Applications 1 How HTML, CSS, and JS Fit Together {css} javascript() Content layer The HTML gives the page structure and adds semantics Presentation

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Overview of Source Code Components Comments Library declaration Classes Functions Variables Comments Can

More information

The Very Basics of the R Interpreter

The Very Basics of the R Interpreter Chapter 2 The Very Basics of the R Interpreter OK, the computer is fired up. We have R installed. It is time to get started. 1. Start R by double-clicking on the R desktop icon. 2. Alternatively, open

More information

COSC 122 Computer Fluency. Programming Basics. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 122 Computer Fluency. Programming Basics. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 122 Computer Fluency Programming Basics Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) We will learn JavaScript to write instructions for the computer.

More information

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors First Program Compilation and Execution Simplifying Fractions Loops If Statements Variables Operations Using Functions Errors C++ programs consist of a series of instructions written in using the C++ syntax

More information

JavaScript I Language Basics

JavaScript I Language Basics JavaScript I Language Basics Chesapeake Node.js User Group (CNUG) https://www.meetup.com/chesapeake-region-nodejs-developers-group START BUILDING: CALLFORCODE.ORG Agenda Introduction to JavaScript Language

More information

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

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style CS125 : Introduction to Computer Science Lecture Notes #4 Type Checking, Input/Output, and Programming Style c 2005, 2004, 2002, 2001, 2000 Jason Zych 1 Lecture 4 : Type Checking, Input/Output, and Programming

More information

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of

More information