Strings If you wish to store a quote inside a string of text than you have a

Strings if you wish to store a quote inside a string

This preview shows page 62 - 69 out of 83 pages.

Strings If you wish to store a quote inside a string of text than you have a few options: Option 01: Alternate your quote types between double and single quotes const someString = ‘The man said “Hello”’; This will not work with the second example string: const anotherString = ‘I’m the person that said “Hello”; This will fail because the apostrophe in “I’m” will cause the initial opening single quote to close after the letter “I” Use option 02 or option 03 for strings which contain various quotes and apostrophes Option 02: Use the “ \ ” escape character to escape the next character in the string Using the “ \ ” escape character tells JavaScript to ignore any special meaning that the next character may have const yetAnotherString = ‘I\’m the person that said “Hello”’; Option 03: Use Template Strings (more on template strings in upcoming slides). Template strings use back ticks ( `` ) to store strings const aString = `I’m the person that said “Hello”`;
Image of page 62
Strings Concatenating string values To concatenate string values means to combine two or more strings together to form a single larger string To combine strings together you use the “+” let a = ‘Hello’ ; let b = ‘World’ ; alert ( a + ‘ ’ + b ); This empty string adds a space between the two words
Image of page 63
Template Strings Another type of string which is new to JavaScript is a template string Template strings are formed by wrapping text in backticks ( `` ) Template strings allow for variables to be placed directly inside the string by using the ${variableName} syntax Below is an example of a template screen that has a variable inside the string const output = `The country is ${ country }` ; Expressions can also be resolved inside template strings Below is an example of an expression being resolved inside a template string const sum = `12 + 6 = ${ 12 + 6 }` ;
Image of page 64
Numbers A number is pretty much just what it says, it is a number The following are numbers 2 0 -3 0.23589 -0.12 6.82 -8.276 Unlike other computer languages integers (whole numbers) and floating point (numbers with decimals points) do not have special or separate data types
Image of page 65
Arithmetic Operators Operator Description Example Value of Quantity = assign quantity = 10 10 + addition quantity = 10 + 6 16 - subtraction quantity = 10 - 6 4 * multiplication quantity = 10 * 2 20 / division quantity = 10 / 2 5 66
Image of page 66
Booleans Variables that have a data type of a Boolean can only store the values of true or false Often used in conditional statements Example: let x = true; if (x == true){ // do something };
Image of page 67
Strings that Look Like Numbers Often in JavaScript we are retrieving data from our users via a form When data is returned from a form to JavaScript the data is always in the form of a string For example if the user enters their age in a form then submits the form the data that is returned is "43" (or whatever age they entered). Notice the quotes around the number. This number looks like a number but is actually a string The problems arise when we try to perform some math functions on the number using the "+" operator. If we try to add the number to 2 to the string number "43" we would get "432" and not 45 as JavaScript thinks "43" is a string so concatenates 2 on to the end of the string
Image of page 68
Image of page 69

You've reached the end of your free preview.

Want to read all 83 pages?

  • Spring '19

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture