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
Related key terms
1 of 5
6 Key excerpts on "Javascript Assignment Operators"
- eBook - PDF
Customizing Vendor Systems for Better User Experiences
The Innovative Librarian's Guide
- Matthew Reidsma(Author)
- 2016(Publication Date)
- Libraries Unlimited(Publisher)
But JavaScript also allows for other mathematical operators: • - for subtraction • * for multiplication • / for division In addition, there are other numeric operators that can be useful. For instance, ++ is used to increment a JavaScript number variable by one, and -- is used to decrement a number variable by one. These can come in handy when running function over a large set of data, like we will do with loops in Chapter 6. If you need to keep track of how many times the loop has run, you can set a variable to increment each time the loop runs, like this: for(i = 0; i < 10; i++) { // Do something } Here the for() loop first sets a variable called i to the numeric value of 0. It then runs the statements inside of the loop as long as the second statement JavaScript Basics 33 is true. As long as i continues to be less than 10, the loop will run. Finally, the last statement increments the value of i by one each time the loop runs. Instead of writing i = i + 1, we can just write i++. Assignment Operators We’ve also seen at least one operator for assigning a value to a JavaScript var- iable: the =. If we want to set the value of the variable myVariable to the phrase “Seashells by the sea shore,” we can use the = to do that: var myVariable = "Seashells by the sea shore"; One useful assignment operator that we will also use is the += variable. This operator assigns the value of a variable to whatever the existing value is plus the new value. In the case of numeric variables, this will be the same as using the addition operator. But in the case of strings, this will concatenate the strings for you. For instance: // Numeric variables var myVariable = 5; myVariable += 15; // Returns 20 // String variables var myVariable = "Today is "; myVariable += "Sunday"; // Returns "Today is Sunday" Comparison Operators Comparison operators are used to compare two values. - eBook - PDF
- Patrick Carey, Sasha Vodnik, Patrick Carey(Authors)
- 2021(Publication Date)
- Cengage Learning EMEA(Publisher)
Creating statements that are easy to read is another instance of self- documenting code. For example, code that you create with the increment operator (++) is more concise than code that assigns a variable a value of itself plus 1. However, in this shortened form, the code can be more challenging to read and understand quickly. For this reason, some developers choose not to use the ++ operator at all, relying instead on the + and = operators to make their code easier for themselves and other developers to read. As you build your programming skills, you’ll learn multiple ways to code many different tasks, but the best option is usually the one that results in code that is easy for you and other programmers to read and understand. This results in code that can be more easily maintained and modified by other developers when you move on to another job. Skills at Work Making Your Code Self-Documenting Assignment Operators An assignment operator is used for assigning a value to a variable. The most common assignment operator is the equal sign (=), but JavaScript supports other operators known as compound assignment operators that both assign a value and perform a calculation. For example, the expression x += y both performs the addition operation and assigns a value so that this expression is equivalent to x = x + y Figure 2-10 lists other compound assignment operators supported by JavaScript. OPERATOR EXAMPLE EQUIVALENT TO 5 x 5 y x 5 y 15 x 15 y x 5 x 1 y 25 x 25 y x 5 x 2 y *5 x *5 y x 5 x * y /5 x /5 y x 5 x/y %5 x %5y x 5 x % y **= x**=y x = x**y Figure 2-10 Assignment operators Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. - eBook - ePub
Clean Code in JavaScript
Develop reliable, maintainable, and robust JavaScript
- James Padolsey(Author)
- 2020(Publication Date)
- Packt Publishing(Publisher)
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
Passage contains an image
Operator arity
Arity refers to how many operands (or inputsIf we consider the greater-than operator (>), it receives two operands: a > b) an operator can receive. An operand is a formal term for the value(s) that you cangive or pass to an operator.In this example, a is its first operand (or left-side operand). And b - eBook - PDF
- Chris Minnick(Author)
- 2022(Publication Date)
- For Dummies(Publisher)
Chapter 5 IN THIS CHAPTER » Reading and coding JavaScript expressions » Changing values with assignment operators » Thinking logically with comparison operators » Doing the math with arithmetic operators » Getting wise to bitwise operators » Putting it together with string operators 226 BOOK 3 Advanced Web Coding Express Yourself An expression is a piece of code that resolves to a value. Expressions can either assign a value to a variable, or they can simply have a value. For example, both of the following are examples of valid expressions: 1 + 1 a = 1; Expressions can be short and simple, as illustrated in these examples, or they can be quite complicated. The pieces of data (1 or a in these examples) in an expression are called operands. Hello, Operator The engines that make expressions do their work are called operators. They oper- ate on data to produce different results. The = and + in the preceding expressions are examples of operators. Operator precedence A single expression often will contain several operators. Consider the following example: a = 1 + 2 * 3 / 4; Depending on the order in which you perform the different calculations, the final value of a could be any one of the following: a = 1.75 a = 2.5 a = 2.25 In fact, the actual result of this expression will be 2.5. But how do you know this? Depending on the person doing the math, the division could be done first (3 / 4), the addition could be done first (1 + 2), or the multiplication could be done first (2 * 3). Working with Operators, Expressions, and Statements CHAPTER 5 Working with Operators, Expressions, and Statements 227 Clearly, there must be a better way to figure out the answer, and there is! This is where operator precedence comes in. Operator precedence is the order in which operators in an expression are evaluated. Operators are divided into groups of different levels of precedence, numbered from 0 to 19, as shown in Table 5-1. - eBook - PDF
- Chris Minnick(Author)
- 2023(Publication Date)
- For Dummies(Publisher)
Working with Operators and Expressions CHAPTER 4 Working with Operators and Expressions 85 x = (4-5-2) / (2*2); The result of this statement is -3 / 4, or -.75. Assignment operators The assignment operator assigns a value to the operand on the left based on the operand on the right: x = 10; You can also chain together assignment operators. For example: x = y = z = 0; Remember that the associativity of the assignment operator is from right to left. So, the way JavaScript executes this expression is by assigning 0 to z, and then the value of z to y, and then the value of y to x. In the end, all three variables are 0. Comparison operators The comparison operators test for equality of the left and right operands, and return a Boolean (true or false) value. Table 4-1 shows the complete list of com- parison operators. TABLE 4-1 JavaScript Comparison Operators Operator Description Example == Equality 3 == “3” // true != Inequality 3 != 3 // false === Strict equality 3 === “3” // false !== Strict inequality 3 !== “3” // true > Greater than 7 > 1 // true >= Greater than or equal to 7 >= 7 // true < Less than 7 < 10 // true <= Less than or equal to 2 <= 2 // true 86 BOOK 1 JavaScript Fundamentals The equality and inequality operators (== and !=, respectively) only compare the values of the left and right operands. When possible, they change the type of the operand on the right to match the operand on the left. This behavior is, from the standpoint of JavaScript, a friendly thing to do. However, it results in some strange behaviors that can cause bugs in programs. For example, using the equality operator, the following statement evaluates to true: 0 == "0" In reality, the number 0 is not the same as a string containing a 0. Relying on JavaScript to automatically convert a string (for example, from a form input) to a number might not break your program, but it’s considered bad coding practice to use a string where you mean to use a number (or vice versa). - eBook - PDF
Web Programming
Building Internet Applications
- Chris Bates(Author)
- 2014(Publication Date)
- Wiley(Publisher)
−= Subtracts the term on the right from the term on the left, then assigns the result to the one on the left of the expression. *= Multiplies two values then assigns the result to the one on the left of the expression. /= Divides the term on the left by the term on the right and then assigns the result to the one on the left of the expression. %= Performs modulus division then assigns the result to the one on the left of the expression. ++ Auto-increment, increases the value of its (integer) argument by one. -- Auto-decrement, decreases the value of an integer by one. Table 6.2 JavaScript operators 3 One is joined to the end of the other. 6.9. Arrays 175 through the code in this book you will see these operators used by both JavaScript and Perl. In fact most programming languages use these same constructs. Instead of giving lots of examples of the use of these operators here, I am going to rely on your finding them as you work through the book and then referring back to this table when you need more information. Generally, though, you will be able to work out from the code what each operator is used for. Although you cannot subtract strings, you can add them. The process is called concate- nation and joins the second string onto the end of the first: 1 var first = "A string is "; var second = "added to the end"; // a new string which is the others added to each other 5 var third = first + second; // change the value of first to be itself + second... first += second; // honestly! 6.9 ARRAYS An array is an ordered set of data elements which can be accessed through a single variable name. Conceptually, an array is made up of a set of slots with each slot assigned to a single data element. You access the data elements either sequentially, by reading from the start of the array, or by their index. The index is the position of the element in the array (with the first element being at position 0 and the last at (array length − 1)).
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.





