JavaScript Basics: Operators & Strings
JavaScript Basics: Operators & Strings
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 .