Expressions in JavaScript, Slides of Mathematics

In JavaScript, each of the functions in the Math library begins with the library name followed by a dot and then the name of the function.

Typology: Slides

2022/2023

Uploaded on 03/01/2023

picoo
picoo 🇮🇳

4.5

(13)

235 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Expressions in JavaScript
Jerry Cain
CS 106AJ
October 1, 2018
slides courtesy of Eric Roberts
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Expressions in JavaScript and more Slides Mathematics in PDF only on Docsity!

Expressions in JavaScript

Jerry Cain

CS 106AJ

October 1, 2018

slides courtesy of Eric Roberts

What is JavaScript?

  • JavaScript was developed at the Netscape Communications Corporation in 1995, reportedly by a single programmer in just 10 days. The language, which was called Mocha at the time, was designed to serve as a programming language that could be embedded in web pages viewed in the browser.
  • JavaScript has since become the dominant language for all interactive web content and appears in some surveys as the most popular language in the computing industry.
  • As always, the focus of any of the CS 106A courses is to teach you the fundamentals of programming, rather than to teach the details of any particular language.
  • To this end, CS 106AJ does not try to teach all of JavaScript, but focuses instead on what the JavaScript expert and evangelist Douglas Crockford calls "the good parts".

Arithmetic Expressions

  • Like most languages, JavaScript specifies computation in the form of an arithmetic expression , which consists of terms joined together by operators.
  • Each term in an arithmetic expression is one of the following:
    • An explicit numeric value, such as 2 or 3. 14159265
    • A variable name that serves as a placeholder for a value
    • A function call that computes a value
    • An expression enclosed in parentheses
  • The operators are typically the familiar ones from arithmetic: + (^) Addition - (^) Subtraction ***** (^) Multiplication / (^) Division % (^) Remainder

The Remainder Operator

  • The result of the % operator make intuitive sense only if both operands are positive integers. The examples in the book do not depend on knowing how % works with negative numbers or numbers that have fractional parts.
  • The remainder operator turns out to be useful in a surprising number of programming applications and is well worth a bit of study.
  • The only arithmetic operator that has no direct counterpart in traditional mathematics is % , which computes the remainder when the first is divided by the second: 14 % (^7) returns 0 14 % 5 returns 4 7 % (^14) returns 7

Variables

  • The simplest terms that appear in expressions are constant literals and variables. A variable is a placeholder for a value that can be updated as the program runs.
  • A variable in JavaScript is most easily envisioned as a box capable of storing a value
  • Each variable has the following attributes:
    • A name , which enables you to tell the variables apart.
    • A value , which represents the current contents of the variable.
  • The name of a variable is fixed; the value changes whenever you assign a new value to the variable. answer 42

Variable Declarations

  • In JavaScript, you must declare a variable before you can use it. The declaration establishes the name of the variable and, in most cases, specifies the initial value as well. let name = value ;
  • The most common form of a variable declaration is where name is an identifier that indicates the name of the variable, and value is an expression specifying the initial value.
  • Most declarations appear as statements in the body of a function definition. Variables declared in this way are called local variables and are accessible only inside that function.

Naming Conventions

  • In JavaScript, all names must conform to the syntactic rules for identifiers, which means that the first character must be a letter and the remaining characters must be letters, digits, or the underscore character.
  • Beyond these rules that apply to all JavaScript names, there are several conventions that programmers use to make their identifier names easier to recognize: - Variable names and function names begin with a lowercase letter. If a name consists of more than one word, the first letter in each word is capitalized, as in numberOfStudents. This convention is called camel case. - Class names and program names begin with an uppercase letter. - Constant names are written entirely in uppercase and use the underscore character to separate words, as in MAX_HEADROOM.

Precedence

  • If an expression contains more than one operator, JavaScript uses precedence rules to determine the evaluation order. The arithmetic operators have the following relative precedence: unary - *** / %
  • -** highest lowest Thus, JavaScript evaluates unary operators first, then the operators ***** , / , and % , and then the operators + and -.
  • Precedence applies only when two operands compete for the same operator. If the operators are independent, JavaScript evaluates expressions from left to right. Parentheses may be used to change the order of operations.

Assignment Statements variable = expression ;

  • You can change the value of a variable in your program by using an assignment statement , which has the general form:
  • The effect of an assignment statement is to compute the value of the expression on the right side of the equal sign and assign that value to the variable that appears on the left. Thus, the assignment statement total = total + value; adds together the current values of the variables total and value and then stores that sum back in the variable total.
  • When you assign a new value to a variable, the old value of that variable is lost.

Shorthand Assignments

  • Statements such as total = total + value; are so common that JavaScript allows the following shorthand: total += value; variable op = expression ;
  • The general form of a shorthand assignment is where op is any of JavaScript’s binary operators. The effect of this statement is the same as variable = variable op ( expression );

Functions Revisited

  • Last week, you learned that a function in Karel is a sequence of statements that has been collected together and given a name.
  • Although that definition also applies in JavaScript, it fails to capture the idea that functions can process information.
  • In JavaScript, a function can take information from its caller, perform some computation, and then return a result.
  • This notion that functions exist to manipulate information and return results makes functions in programming similar to functions in mathematics, which is the historical reason for the name.

Functions in Mathematics

  • Plugging in a value for x allows you to compute the value of f ( x ), as follows:
  • The graph at the right shows the values of the function f ( x ) = x 2 - 5 f (0) = 0 2 - 5 = - 5 f (1) = 1 2 - 5 = - 4 f (2) = 2 2 - 5 = - 1 f (3) = 3 2 - 5 = 4
  • The JavaScript version of f ( x ) is function f(x) { return x * x – 5; }

Examples of Simple Functions

  • The following function converts Fahrenheit temperatures to their Celsius equivalent: function fahrenheitToCelsius(f) { return 5 / 9 * (f – 32); }
  • The following function computes the area of a triangle from its base and height: function triangleArea(base, height) { return (base * height) / 2; }

Libraries

  • To make programming easier, all modern languages include collections of predefined functions. Those collections are called libraries.
  • For programming that involves mathematical calculations, the most useful library is the Math library, which includes a number of functions that will be familiar from high-school mathematics (along with many that probably aren’t). A list of the most important functions appears on the next slide.
  • In JavaScript, each of the functions in the Math library begins with the library name followed by a dot and then the name of the function. For example, the function that calculates square roots is named Math.sqrt.
  • You call library functions just like any other function, so that calling Math.sqrt(16) returns the value 4.