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

Math. ○ Math in JavaScript uses the follow operators. – Addition: +. – Subtraction: - ... Functions allow us to write code that reacts to.

Typology: Summaries

2022/2023

Uploaded on 03/01/2023

rajeshi
rajeshi 🇺🇸

4.1

(9)

237 documents

1 / 55

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
JavaScript
CS 0134
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37

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 pageIn HTML5, by default, the browser assumes any scripts defined by the script element to be written in JavaScriptYou 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 pagesLike 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 consoleThe console lets you see messages, warnings and errors related to your web pageYou open the console with Ctrl+Shift+K (Cmd+Option+K) in Firefox

Variables

Variables are used to store dataTo define a variable you use the keyword let followed by a variable nameNaming 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 scriptJavaScript variables have scope, more on that laterJavaScript 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 notationIn addition to explicit numbers, a number can be represented with -Infinity, +Infinity and NaN (not a number)

Booleans

Booleans are the values true and falseExamples

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

Null and undefined

null represents no valueUndefined happens when a variable has been declared but no value has been definedExamples

  • (^) 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 bracketsAn 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 arrayExample

  • (^) 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: --