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

4 Key excerpts on "Javascript Assignment Operators"

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.
  • HTML, CSS & JavaScript in easy steps

    ...All except the simple = assignment operator are shorthand forms of longer expressions, so each equivalent is also given for clarity. Operator Example Equivalent = a = b a = b += a += b a = (a + b) –= a –= b a = (a – b) *= a *= b a = (a * b) /= a /= b a = (a / b) %= a %= b a = (a % b) **= a **= b a = (a ** b) It is important to think of the = operator as meaning “assign” rather than “equals” to avoid confusion with the JavaScript === equality operator. In the = example in the table, the variable a gets assigned the value contained in variable b to become its new stored value. The combined += operator is most useful and can be employed to append a string onto an existing string. For example, with a variable string let str = ‘JavaScript’ and str += ‘ Fun’ the variable now stores the combined string ‘JavaScript Fun’. Numerically speaking, the += example in the table will add the value contained in variable a to that contained in variable b then assign the sum total to become the new value stored in variable a. All other combined assignment operators work in a similar way to the += operator. They each perform the arithmetical operation on their two operands first, then assign the result of that operation to the first variable – so that becomes its new stored value. The === equality operator compares values and is fully explained here. Create an HTML document with a self-invoking anonymous function that concatenates two strings (function () { let msg = ‘ JavaScript ’ ; msg += ‘ Fun ’ console.log(‘ Add & concatenate: ‘ + msg) // Statements to be inserted here. }) () assignment.html Next, insert statements that use combined operators to perform arithmetic and assign results for output let sum = 5.00 ; sum += 2.50 console.log(‘ Add & assign decimal: ‘ + sum) sum = 8 ; sum -= 4 console.log(‘ Subtract & assign integer: ‘...

  • Confident Web Design
    eBook - ePub

    Confident Web Design

    Master the Fundamentals of Website Creation and Supercharge Your Career

    • Kenny Wood(Author)
    • 2018(Publication Date)
    • Kogan Page
      (Publisher)

    ...When you assigned the variable, you made use of the assignment operator ‘=’. Arithmetic operators There are many different operators available to us. The arithmetic-based operators are as follows: + Addition. Adds numbers together. - Subtraction. Subtracts numbers from one another. Multiplication. Multiplies numbers together. / Division. Divides numbers from each other. % Modulus. Returns the remainder left after a division. For example, 10% 3 would be 1 because 3 * 3 gets you to 9, leaving the remainder 1 to get to 10. ++ Increment. This will add 1 to your number. – Decrement. This will subtract 1 from your number. We can combine any number of these together to form an output, like so: // value of x would be 10 var x = 4 * 5 / 2; We can also use parenthesis to help formulate our formula, like so: // value of x would be 2 var x = (4 * 5) / (2 * 10); We can also substitute values for variables and use the same operators. This is where programming can become very useful, with these reusable values. Let’s see an example: var x = 4; var y = 8; // value of i would be 12 var i = x + y; You can mix and match both variables and numbers in an expression with the same effect, like so: var x = 4; var y = 8; // value of i would be 20 var i = x + y + 8; These arithmetic operators are used extensively when programming with JavaScript. Their uses are endless, but in their simplest form they can be used to create a new number from a user’s input. Let’s imagine for a second that we wanted to create a website where the user could enter their age and find out roughly how many days they had been alive. Let’s work through how this would be possible with JavaScript below: Exercise Let’s put this into practice and get you thinking like a programmer...

  • Coding + Math
    eBook - ePub

    Coding + Math

    Strengthen K–5 Math Skills With Computer Science

    ...This chapter explains how math operators are used in CS and what similarities and differences exist between the two disciplines. The chapter also explores loops and how they function in programming. Included in this chapter: • Project using the tool Flowgorithm to create loops • Comparison of loop function across programs • Resources for deeper exploration of operators and loops Operators Knowledge of basic math operators transfers effectively to a programming environment due to the fact that their use in coding mirrors their frequent use in math learning. Addition and subtraction use the same symbols (+, –) and operate for the most part just as they do in standard math exercises. Multiplication and division use different symbols than students may be used to (* and / rather than x and ÷, respectively), but otherwise operate the same as students have become accustomed to using them. Likewise, relational operators (e.g., <, >, =, >=, <=) appear and behave exactly as they do in elementary math textbooks and commonly encountered exercises. Students can build confidence in their programming skills while strengthening their math proficiency by programming tasks that utilize these skills in solving problems or executing tasks that require them. This can provide relevance to skills that are sometimes learned in isolation, which can enhance students’ motivation to increase math proficiency as well. The use of the equals sign (=) can cause some confusion for the novice programmer, as it is interpreted differently in code. In math, a = 4 means the two are equal, as in a is already equal to 4. In programming, the equals sign (=) is an assignment operator rather than part of an existing equation. In programming, a = 4 is interpreted as “let a = 4” and is used to assign values to variables...

  • After Effects Expressions
    • Marcus Geduld(Author)
    • 2013(Publication Date)
    • Routledge
      (Publisher)

    ...When I write the mathematical statement 1 + 1 = 2 I’m implying that, in some way, 1 + 1 and 2 mean the same thing: they’re equal to each other. But if I write var xPosition = 15; I don’t mean that xPosition and 15 are the same thing. After all, xPosition is a storage container (a tool); 15 is what I’m storing in that container (data). In JavaScript, the equal sign is called the assignment operator. “var xPosition = 15” means “I’m assigning 15 to the variable xPosition.” You could also call it the storage operator: “I’m storing 15 in xPosition.” If you have a pot with chicken soup stored in it, there’s nothing stopping you from dumping the soup into the trash and replacing it with a totally different kind of soup, maybe French onion. However, 1 + 1 will always equal 2. Hopefully, you’ll never store tennis shoes in a soup pot. Similarly, here’s something you’re not allowed to store in a variable: var xPosition = $; You can’t store a dollar sign in a variable. You can only store numbers, strings, Booleans, arrays, and objects. Numbers You know what a number is. But just to make sure you’re thinking about the full range of possibilities, check out the following legal statements, all of which are numbers or resolve to numbers: var xPosition = 150; var yPosition = 23 + 4; var spinAmount = 10 − (3 * 4); var offset = 23.2; var width = − 17; var rollOfTheDice = ease(time,0,10,0,360); Strings String is short for “string of characters.” A character is anything you can type on your keyboard: b, B, 2, $, and so on. Strings are multiple characters strung together: cat, dog, 123, 99skidoo, @#% &!, and the like. In JavaScript, you always wrap strings in quotation marks (double or single quotes): “cat”, ‘dog’, “123”, “99skidoo,” and “@#%&!” to name a few. Why? Because many strings look suspiciously like variables. They’re not variables. Variables are tools for storing data; strings are data...