0% found this document useful (0 votes)
6 views13 pages

JavaScript Basics: Operators & Strings

This document provides an introduction to basic JavaScript concepts through a series of coding examples grouped into levels. It covers numeric operators, strings, comparisons, and special characters. The examples show how to perform calculations and manipulate text using JavaScript syntax and functions. Key concepts demonstrated include data types, concatenation, ordering operations, and accessing string properties.

Uploaded by

asheeshmisra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

JavaScript Basics: Operators & Strings

This document provides an introduction to basic JavaScript concepts through a series of coding examples grouped into levels. It covers numeric operators, strings, comparisons, and special characters. The examples show how to perform calculations and manipulate text using JavaScript syntax and functions. Key concepts demonstrated include data types, concatenation, ordering operations, and accessing string properties.

Uploaded by

asheeshmisra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

THE LAST GREAT UNCHARTED FRONTIER

The CLIFFS OF VALUE


Level 1
LEVEL ONE
THe Prompt & some Basic Numbers
24
>
24
3.14
>
3.14
The JavaScript Prompt, aka the Console >
What gets returned from the code
JavaScript automatically recognizes numbers
LEVEL ONE
Operators
10
Common Operators used in JavaScript Syntax:
> 6 + 4
12
> 3 * 4
4
> 9 - 5
3
> 12 / 4
addition subtraction
multiplication division
3
> 43 % 10
modulus
Modulus returns the
remainder after division.
LEVEL ONE
Order of Operations: PEMDAS
36
Grouping Expressions in JavaScript
> (5 + 7) * 3
12 * 3
9
> (3 * 4) + 3 - 12 / 2
12 + 3 - 12 / 2
12 + 3 - 6
-16
> (-5 * 6) - 7 * -2
-30 - 7 * -2
4
> 4 + (8 % (3 + 1))
4 + (8 % 4 )
4 + 0
-30 - -14
LEVEL ONE
Comparators
true
Common Number Comparators used in JavaScript Syntax:
> 6 > 4
false
> 3 == 4
false
> 9 < 5
true
> 12 != 4
greater than less than
equals not equals
boolean value
two equal signs!
the not symbol
true
greater or equal
> 8 >= -2
true
less or equal
> 10 <= 10
LEVEL ONE
Strings
How JavaScript stores and processes !at text
>
>
"Raindrops On Roses"
"Raindrops On Roses" + " And " + "Whiskers On Kittens"
>"Whiskers On Kittens"
"Raindrops On Roses" "Whiskers On Kittens"
"Raindrops On Roses And Whiskers On Kittens"
Plus will glue Strings together
Strings need quotes!
LEVEL ONE
These are a few of my favorite...Strings
Concatenation works with numbers and their expressions, too.
>"The meaning of life is " + 42
"The meaning of life is 42"
Notice the extra space!
>"The meaning of life is" + 42
"The meaning of life is42"
Uh oh...what happened?
Concatenation adds no
spaces, so we need to
add our own.
LEVEL ONE
These are a few of my favorite...Strings
Concatenation works with numbers and their expressions, too.
"Platform " + 9 + " and " + 3/4
"Platform 9 and 0.75"
Expressions get evaluated!
>
Make strings out of
expressions that you
want to see in their
original format.
"Platform " + 9 + " and 3/4" >
"Platform 9 and 3/4"
LEVEL ONE
Special characters inside Strings
Some characters need backslash notation in JavaScript Strings
>
>
advances to the next tab stop
"Flight #:\t921\t\tSeat:\t21C"
"Flight #: 921 Seat: 21C"
Adds a quotation mark but without
ending the string too early.
"Login Password:\t\t\"C3P0R2D2\""
"Login Password: "C3P0R2D2""
LEVEL ONE
Special characters inside Strings
Some characters need backslash notation in JavaScript Strings
>
>
shifts the printout to a new line
"Origin\\Destination:\tOrlando(MCO)\\London(LHR)"
"Departure:\t09:55A\nArrival:\t14:55P"
"Origin\Destination: Orlando(MCO)\London(LHR)"
Places a backslash itself in the String
"Departure: 09:55A
Arrival: 14:55P"
LEVEL ONE
String Comparisons
Checking for matching strings and alphabetical ordering
>
>
"The Wright Brothers" == "Super Mario Brothers"
"The Wright Brothers" != "the wright brothers"
false
true
Case counts!
> "The Wright Brothers" == "The Wright Brothers"
true
Double equals will compare EXACT contents
Case counts!
Not equals returns true if there is a mismatch
LEVEL ONE
String Comparisons
The length of strings can be accessed with the .length property
28
>
Case counts!
"antidisestablishmentarianism".length
39
> "One Fish, Two Fish, Red Fish, Blue Fish".length
Spaces and any non-alphabetic
characters are counted, too!
Returns a number value

Common questions

Powered by AI

JavaScript uses escape sequences to handle special characters within strings, where \n represents a newline and \t represents a tab. These allow developers to format strings across multiple lines or add tabbed spaces without breaking the string's syntax structure. For example, "Departure:\t09:55A\nArrival:\t14:55P" produces formatted multiline output .

JavaScript uses backslash notation, such as \ for backslashes, \n for new lines, or \t for tabs, to properly insert these special characters within strings. The backslash serves as an escape character, indicating that the following character should be treated differently. Incorrect use, like omitting the backslash, can prevent proper execution, display unexpected results, or cause syntax errors .

JavaScript concatenates strings and numbers by converting numbers to strings. This process does not add spaces automatically, which can lead to unexpected outputs visually compacted. For example, expressions like 'The meaning of life is ' + 42 result in 'The meaning of life is42' without a space before 42. It's necessary to explicitly include space within the string to achieve the desired formatting .

The modulus operator '%' in JavaScript returns the remainder after division of one number by another, e.g., 43 % 10 equals 3. It is particularly useful in scenarios such as determining even or odd numbers, where a number % 2 results in 0 for even or 1 for odd, or in cyclic operations like rotating elements in arrays, where wrapping index calculations are required .

JavaScript evaluates comparative expressions by determining the relationships between numbers. For instance, '6 > 4' evaluates to true because 6 is greater than 4, whereas '3 == 4' evaluates to false because 3 is not equal to 4. These expressions are critical for decision-making processes in scripts .

In JavaScript, string comparisons are case-sensitive, meaning that case differences are considered significant. For example, 'The Wright Brothers' != 'the wright brothers' will return true because the uppercase 'T' and 'W' are not the same as the lowercase 't' and 'w'. This can impact conditions where exact matches are necessary, such as password validations or search queries .

JavaScript uses the PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction) rule to prioritize operations in arithmetic expressions. Parentheses are evaluated first, followed by multiplication and division from left to right, and finally addition and subtraction from left to right. This hierarchy affects how complex expressions are calculated; improper arrangement or omission of parentheses could lead to dramatically different results in an expression evaluation .

The '==' operator in JavaScript checks if two strings are identical in content and returns true if they are, while '!=' checks if two strings differ in content and returns true if they do not match. These operators are case-sensitive, meaning that even a difference in letter casing results in false or true outcomes, respectively .

The .length property in JavaScript returns the total number of characters in a string, including spaces and non-alphabetic characters. This can affect logic in user input validation, where length constraints may involve counting spaces or punctuation. For instance, 'antidisestablishmentarianism'.length results in 28, while "One Fish, Two Fish" includes all spaces and characters in its length .

JavaScript treats the '+' operator as both an arithmetic addition and a string concatenation operator. When used between numbers, it performs arithmetic addition, e.g., 6 + 4 results in 10. When used between strings or a string and a number, it performs concatenation, e.g., '6' + '4' results in '64'. The presence of number expressions adjacent to strings undergoes conversion, impacting the result accordingly .

You might also like