Computer Science

Javascript Assignment Operators

JavaScript assignment operators are used to assign values to variables. They combine the assignment operator (=) with other operators to perform an operation and then assign the result to the variable. For example, the += operator adds a value to the variable's current value. These operators provide a shorthand way to perform operations and assignment in a single step.

Written by Perlego with AI-assistance

5 Key excerpts on "Javascript Assignment Operators"

  • Clean Code in JavaScript
    eBook - ePub

    Clean Code in JavaScript

    Develop reliable, maintainable, and robust JavaScript

    dynamic typing , we explored topics such as type-coercion and detection; we also covered several operators. In this chapter, we'll continue this exploration by delving into every single operator that the JavaScript language makes available. Having a rich understanding of JavaScript's operators will make us feel utterly empowered in a language that can, at times, appear confusing. There is, unfortunately, no shortcut to understanding JavaScript, but as you begin to explore its operators, you will see patterns emerge. For example, many of the multiplicative operators work in a similar manner, as do the logical operators. Once you are comfortable with the main operators, you will begin to see that there is a grace underlying the complexity.
    It may be useful to treat this chapter as more of a reference if you're pressed for time. Do not feel like you need to exhaustively retain every detail of every operator's behavior. In this chapter, we will cover the following topics:
    • What is an operator?
    • Arithmetic and numeric operators
    • Logical operators
    • Comparative operators
    • Assignment operators
    • Property access operators
    • Other operators and syntax
    • Bitwise operators
    Now that we're ready to dive in, the very first question we need to ask ourselves is: what even is an operator?
    Passage contains an image

    What is an operator?

    An operator in JavaScript is a standalone piece of syntax that forms an expression and is typically used to derive something or compute a logical or mathematical output from a set of inputs (called operands ).
    Here, we can see an expression containing an operator (+) with two operands (3 and 5): 3 + 5 Any operator can be said to have four characteristics:
    • Its arity : how many operands the operator accepts
    • Its function : what the operator does with its operands and what it evaluates to
    • Its precedence : how the operator will be grouped when used in combination with other operators
    • Its associativity : how the operator will behave when neighbored with operators of the same precedence
    It's important to understand these foundational characteristics as it will vastly aid your usage of operators in JavaScript.
    Passage contains an image

    Operator arity

    Arity refers to how many operands (or inputs
    ) an operator can receive. An operand is a formal term for the value(s) that you can
    give or pass to an operator.
    If we consider the greater-than operator (>), it receives two operands: a > b
    In this example, a is its first operand (or left-side operand). And b
  • Decoding JavaScript
    eBook - ePub

    Decoding JavaScript

    A Simple Guide for the Not-so-Simple JavaScript Concepts, Libraries, Tools, and Frameworks (English Edition)

    JavaScript (JS) operators allow you to do mathematical operations, combine two strings, compare values, determine logic between variables or values, and also find the data type of a variable. Since this book caters specifically towards advanced JavaScript, we expect the readers to have a basic idea of JavaScript operators. While we will go through the theory of the basic JS concepts, and also look at one or two examples, we encourage the readers to practices different patterns on their own to refresh their knowledge of these basic concepts.
    There are the following six major types of operators in JavaScript:
    • Arithmetic Operators : They are used to perform arithmetic operations on numbers. Arithmetic operators include + (addition), - (subtraction), * (multiplication), / (division), % (modulus, the remainder of a division), ++ (increment), and – (decrement).
      1. var sum = 4 + 3;      //Output will be 7
        Code 1.15: Example of arithmetic operators
    • Assignment Operators : These operators are used for assigning values to the JavaScript variables.
      Operator Example Meaning
      = a = 5 a = 5
      += a += 5 a = a + 5
      -= a -= 5 a = a - 5
      *= a *= 5 a = a * 5
      /= a /= 5 a = a / 5
      %= a %= 5 a = a % 5
      **= a **= 5 a = a ** 5
      Table 1.2: Assignment Operators
    • String operators : When the + operator is used on strings; it concatenates two strings.
      1. var txt1 = 'Hello';
      2. var txt2 = 'World';
      3. var greeting = txt1 + txt2; //HelloWorld
        Code 1.16: Example of string operators
      Note: When you use the + operator on a number and a numeric string, the answer will be a string. For example, 5 + '5' will be '55'. However, if use any other arithmetic operator on a combination of strings and numeric strings, the arithmetic operation will succeed, and the answer will be numeric. For instance, '25'/5 will give 5.
    • Comparison operators : These operators are used for comparing two or more variables and/or values. The answer to the operations using comparison operators are boolean values. The various comparison operators are as follows:
      Operator Example Result
      == (check equality) 5 == '5' true
      === (check for equal value AND type) 5 === '5' false
      != (check for non-equality) 5 != '5' false
      !== (check for non-equal value AND type)
  • JavaScript from Beginner to Professional
    • Laurence Lars Svekis, Maaike van Putten, Codestars By Rob Percival(Authors)
    • 2021(Publication Date)
    • Packt Publishing
      (Publisher)

    2

    JavaScript Essentials

    In this chapter, we will be dealing with some essential building blocks of JavaScript: variables and operators. We will start with variables, what they are, and which different variable data types exist. We need these basic building blocks to store and work with variable values in our scripts, making them dynamic.
    Once we've got the variables covered, we will be ready to deal with operators. Arithmetic, assignment, and conditional and logical operators will be discussed at this stage. We need operators to modify our variables or to tell us something about these variables. This way we can do basic calculations based on factors such as user input.
    Along the way, we'll cover the following topics:
    • Variables
    • Primitive data types
    • Analyzing and modifying data types
    • Operators
      Note: exercise, project, and self-check quiz answers can be found in the Appendix .

    Variables

    Variables are the first building block you will be introduced to when learning most languages. Variables are values in your code that can represent different values each time the code runs. Here is an example of two variables in a script:
    firstname = "Maaike" ; x = 2 ;
    And they can be assigned a new value while the code is running:
    firstname = "Edward" ; x = 7 ;
    Without variables, a piece of code would do the exact same thing every single time it was run. Even though that could still be helpful in some cases, it can be made much more powerful by working with variables to allow our code to do something different every time we run it.

    Declaring variables

    The first time you create a variable, you declare it. And you need a special word for that: let , var , or const . We'll discuss the use of these three arguments shortly. The second time you call a variable, you only use the name of the existing variable to assign it a new value:
    let firstname = "Maria" ; firstname = "Jacky" ;
    In our examples, we will be assigning a value to our variables in the code. This is called "hardcoded" since the value of your variable is defined in your script instead of coming dynamically from some external input. This is something you won't be doing that often in actual code, as more commonly the value comes from an external source, such as an input box on a website that a user filled out, a database, or some other code that calls your code. The use of variables coming from external sources instead of being hardcoded into a script is actually the reason that scripts are adaptable to new information, without having to rewrite the code.
  • LPI Web Development Essentials Study Guide
    • Audrey O'Shea(Author)
    • 2023(Publication Date)
    • Sybex
      (Publisher)
    Assign a value to the current value plus another value a += b a = (a + b) *= Assign a value to the current value multiplied by another value a *= b a = (a * b) ‐= Assign a value to the current value minus another value a ‐= b a = (a ‐ b) /= Assign a value to the current value divided by another value a /= b a = (a / b)
    Precedence means the order in which something is done. When referring to arithmetic operators, it's the order in which the calculations are performed. Perhaps you learned PEMDAS in math class? It stands for parenthesis, exponents, multiplication, division, addition, and subtraction. In JavaScript it's essentially the same, with a few twists. Consider the following:
    x=(1+2*3-4)
    Parentheses are first, but that's irrelevant here. Multiplication happens before addition, so the value is 2*3=6 ; addition is before subtraction, so 6+1=7 ; and finally 7‐4=3 , hence x=3 . If you were unaware of the order of precedence you might work from left to right and calculate the wrong answer (1+2=3 , 3*3=9 , 9‐4=5 ), x=5 .
    If the statement were written as x=((1+2)*3‐4) , then the value of x would indeed be 5.
    Ensure that you pay attention to the order of precedence in your programming. Other actions such as functions and ++ and ‐‐ also have a prescribed order. An excellent online reference can be found at http://w3schools.com/js/js_precedence.asp .

    Data Conversions

    Data conversion happens when a bit of data starts out as one primitive type but becomes another along the way as it is interpreted by JavaScript. Data coercion is another name for data conversion. Implicit conversions are conversions that happen automatically due to how JavaScript itself works. Explicit conversions are those that you intentionally program into your code. This section explains how implicit conversions work.
    Strings to Values
    A string such as "10" will be converted to a value when used with the , / , or *
  • Multimedia Web Design and Development
    eBook - ePub

    Multimedia Web Design and Development

    Using Languages to Build Dynamic Web Pages

    y is:
    Alternatively, you can combine these declarations into a single line by separating the variable names with a comma:
    6.1.2 Assigning Values
    When you initially declare a variable in JavaScript, it has the value undefined. This means that no computations can be performed on the variable, or the results will also have the value undefined. To assign an actual value to a variable, you use the equals sign (=) followed by the value you wish to store. There are a variety of data types that can be stored in a JavaScript variable. The most common ones include the following:
    •  Boolean values: These are true and false, which are most commonly used for evaluating conditional statements. These are reserved words (words that are part of the language itself) in JavaScript, so they can be typed as values without annotation.
    •  Integer and decimal values: These are numeric values that may or may not have a decimal component after them. Literal values do not require annotation and can be typed directly as a stored value.
    •  Characters: Each of these is a single symbol from the alphabet, the digits 0 through 9, or punctuation. A character must be wrapped in quotation marks (such as ‘a’); by convention, characters use single quotation marks and strings use double quotation marks, but either is valid syntax in JavaScript.
    •  Strings: These are combinations of characters stored as a single value. A string must be wrapped in quotation marks (such as “Hello, World!”). By convention, strings are wrapped in double quotation marks, but both double and single quotation marks are valid syntax in JavaScript.
    You can use comments in JavaScript to annotate code. A single-line comment in JavaScript is denoted by //, which tells the browser to ignore the rest of the line. Alternatively, using /* starts a multiline comment that ends only with a corresponding */. An example of the syntax for these comments follows:
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.