Docsity

Introduction to JavaScript: Variables, Data Types, and Control Flow, Summaries of Algebra

Partial preview of the text

Download Introduction to JavaScript: Variables, Data Types, and Control Flow and more Summaries Algebra in PDF only on Docsity!

JavaScript

CS 0134

JavaScript

● JavaScript is a programming language that was designed to run in your web browser.

Script element

● The script element is used to define client-side scripts for a web page ● In HTML5, by default, the browser assumes any scripts defined by the script element to be written in JavaScript ● You can define JavaScript with the script element in two ways

  • (^) Place the code inside the script element as a child
  • (^) Use the src attribute to point to a separate file that contains the code

Script element continued

● While not required, it is beneficial to place the script element as the last child before the body’s closing tag as this speeds up the loading of your web pages ● Like CSS, it is advisable to keep your JavaScript in a separate file

Code in a separate file

● script.js console.log("Hello World!"); ● **In HTML file just before closing body tag **

The console

● Modern desktop web browsers come with a console ● The console lets you see messages, warnings and errors related to your web page ● You open the console with Ctrl+Shift+K (Cmd+Option+K) in Firefox

Variables

● Variables are used to store data ● To define a variable you use the keyword let followed by a variable name ● Naming variables

  • (^) Names are made up of letters, numbers and underscores.
  • (^) The first character of a name must be a letter or underscore
  • (^) Do not use one of the language's keywords as a variable name as this can cause confusion, you can find a list here, https://developer.mozilla.org/en-US/docs/Web/JavaScript/R eference/Lexical_grammar#Keywords

Variables continued

● You can assign a value to a variable using the form "variable = value" ● You can do this assignment both when you define the variable as well as further on in the script ● JavaScript variables have scope, more on that later ● JavaScript is a language that uses dynamic typing. What this means is that you can assign any value to a variable and change the type of the value throughout the script

Types of data

● JavaScript has the following types of data that we can work with

  • (^) Numbers
  • (^) Strings
  • (^) Booleans
  • (^) Null
  • (^) Undefined
  • (^) Arrays

Numbers

● Numbers are numerical values between -( 53

- 1) and ( 53 - 1) (also known as -9,007,199,254,740,991 and 9,007,199,254,740,991) ● Numbers can be written as integers, decimals and scientific notation ● In addition to explicit numbers, a number can be represented with -Infinity, +Infinity and NaN (not a number)

Booleans

● Booleans are the values true and false ● Examples

  • (^) result = true;
  • (^) passed = false;

Null and undefined

● null represents no value ● Undefined happens when a variable has been declared but no value has been defined ● Examples

  • (^) result = null;
  • (^) let a; console.log(a); //will print undefined

Arrays continued

● You can access a value in an array by using the arrays variable name followed by the values index in brackets ● An array index is a numerical value that points to where the value exists in the array; The index for the first value in an array is 0 and increments for every value in the array ● Example

  • (^) a = [1, 2, 3, 4]; console.log(a[0]);

Math

● Math in JavaScript uses the follow operators

  • (^) Addition: +
  • (^) Subtraction: -
  • (^) Multiplication: *
  • (^) Division: /
  • (^) Exponentiation: ** (does not work in IE)
  • (^) Remainder: %
  • (^) Increment by 1: ++
  • (^) Decrement by 1: --