Friday, January 17, 2025
HomeComputer ScienceWhat Are The Special Characters In JavaScript?

What Are The Special Characters In JavaScript?

In JavaScript, special characters are symbols that have specific meanings or functions within the language syntax. Here are some important special characters:

  1. Quotation Marks:
    • " (double quote)
    • ' (single quote) These are used to define string literals in JavaScript.
  2. Backslash (\):
    • Used as an escape character to represent special characters inside strings (e.g., \n, \t, \\, \").
    • Example: var str = "Hello\nWorld"; (newline inside a string).
  3. Dollar Sign ($):
    • Often used in variable names, especially in jQuery, and as part of template literals for embedding expressions.
    • Example: let name = 'Alice'; console.log(Hello, ${name});
  4. Period (.):
    • Used to access properties or methods of an object.
    • Example: obj.property.
  5. Comma (,):
    • Separates values in arrays, function parameters, or argument lists.
    • Example: let arr = [1, 2, 3];
  6. Semicolon (;):
    • Used to terminate statements (though it is optional in many cases due to automatic semicolon insertion).
    • Example: let x = 10;
  7. Colon (:):
    • Used in object literals to define key-value pairs.
    • Example: let obj = { name: "Alice", age: 25 };
  8. Parentheses (()):
    • Used for function calls, grouping expressions, and defining function parameters.
    • Example: function greet() { console.log('Hello'); }
  9. Curly Braces ({}):
    • Used to define blocks of code, object literals, and class bodies.
    • Example: if (x > 5) { console.log('x is greater than 5'); }
  10. Square Brackets ([]):
    • Used to define arrays or access elements in arrays and objects.
    • Example: let arr = [1, 2, 3]; or arr[0].
  11. Angle Brackets (<>):
    • Not commonly used in JavaScript syntax directly, but can appear in certain templating engines or in JSX (React).
  12. Comparison Operators:
    • ==, ===, !=, !==, >, <, >=, <= for comparison.
    • Example: if (x == 10) { console.log("Equal"); }
  13. Logical Operators:
    • && (AND), || (OR), ! (NOT).
    • Example: if (x > 5 && x < 10) { console.log("x is between 5 and 10"); }
  14. Assignment Operator (=):
    • Used to assign values to variables.
    • Example: let x = 10;
  15. Arrow (=>):
    • Used to define arrow functions in ES6+.
    • Example: const add = (a, b) => a + b;
  16. Tilde (~):
    • Bitwise NOT operator.
    • Example: let x = ~5; (flips the bits of 5).
  17. Caret (^):
    • Bitwise XOR operator.
    • Example: let x = 5 ^ 3;
  18. Pipe (|):
    • Bitwise OR operator.
    • Example: let x = 5 | 3;
  19. Ampersand (&):
    • Bitwise AND operator.
    • Example: let x = 5 & 3;
  20. Slash (/):
    • Used for division or as the start of a comment (single-line comment: // and multi-line comment: /* */).
    • Example: let result = 10 / 2;
  21. Percent (%):
    • Used for the modulo (remainder) operation.
    • Example: let remainder = 10 % 3;
  22. Double Asterisk (**):
    • Used for exponentiation (introduced in ES6).
    • Example: let x = 2 ** 3;
  23. Question Mark (?):
    • Ternary conditional operator (condition ? value_if_true : value_if_false).
    • Example: let result = x > 5 ? 'Greater' : 'Smaller';
  24. Exclamation Mark (!):
    • Logical NOT operator.
    • Example: let isTrue = !false;

These special characters play a significant role in the syntax and operation of JavaScript.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x