Computer Science

Javascript Operators

JavaScript operators are symbols used to perform operations on variables and values. They can be used for arithmetic, comparison, logical, assignment, and more. Examples of operators include + for addition, - for subtraction, and === for strict equality comparison. Understanding and using operators is fundamental for writing efficient and functional JavaScript code.

Written by Perlego with AI-assistance

5 Key excerpts on "Javascript 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.
  • 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...

  • HTML, CSS & JavaScript in easy steps

    ...+ sum) sum = 8 ; sum *= 4 console.log(‘ Multiply & assign integer: ‘ + sum) sum = 8 ; sum /= 4 console.log(‘ Divide & assign integer: ‘ + sum) sum = 8 ; sum %= 4 console.log(‘ Modulus & assign integer: ‘ + sum) Save the HTML document, then open it in your browser and launch the console to see the assigned values Make Comparisons The operators that are commonly used in JavaScript to compare two values are all listed in the table below: Operator Comparison === Equality !== Inequality > Greater than < Less than >= Greater than or equal to <= Less than or equal to The === equality operator compares two operands and will return a. Boolean true value if they are exactly equal, otherwise it will return a Boolean false value. If the operands are identical numbers they are equal; if the operands are strings containing the same characters in the same positions they are equal; if the operands are Boolean values that are both true, or both false, they are equal. Conversely, the !== inequality operator returns true if the two operands are not equal, using the same rules as the === equality operator. Equality and inequality operators are useful in comparing two values to perform “conditional branching”, where the script will follow a particular direction according to the result. The > greater than operator compares two operands and returns true if the first is greater in value than the second. The < less than operator makes the same comparison but returns true when the first is less in value than the second...

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

    ...Those sure look like index numbers. Actually, there’s no contradiction. In JavaScript, arrays are special types of objects. Whereas most objects have properties and methods, array objects have properties, methods, and index numbers. By the way, when we access myArray.length, we’re accessing the length property of the myArray object. Math is an object. When I want to round down, I call its floor method, as in Math.floor(17.23). In doggy JavaScript, we might access a breed property, as in Rover.breed. Or we might call a fetch method, as in Rover.fetch(“stick”). JavaScript also lets you store objects in variables; for example, var myComp = thisComp; You can also use variables to store properties and the values spit out by methods. In Chapter05.aep, Comp4, on the only layer’s Opacity property, I’ve typed this silly Expression: var myComp = thisComp; var myLayer = myComp.layer(“clover”); var myGroup = myLayer.transform; var myProperty= myGroup.rotation; myProperty That Expression does the same thing as thisComp.layer(“clover”). transform.rotation Operators Operators are little “machines” that alter or compare values. The ones you know best are the arithmetic operators: +, −, *, and /. Those are called, respectively, the addition operator, the subtraction operator, the multiplication operator, and the division operator. The values they operate on are called operands. For instance, in the statement var myAge = 40 + 3; + is the operator; 40 and 3 are the operands. Here are a few lesser-known but important operators: + =, − =, * =, and / =. + = is just a shorthand for adding two numbers together...

  • Designing and Developing Robust Instructional Apps
    • Kenneth J. Luterbach(Author)
    • 2018(Publication Date)
    • Routledge
      (Publisher)

    ...Technically, as discussed in Section 3.1.4, such commands are calls to subroutines, which many programmers also refer to as functions or methods. In the Spider-Monkey version of JavaScript, we use print for output and readline for input. Other programming languages are similar in this regard. For example, the Pascal programming language uses write for output and readln for input; Perl uses print for output and <stdin> for input; BASIC and Python use print for output and input for input. To summarize this section, we have written interactive programs in JavaScript to obtain the user’s name and the user’s response to a question. Those programs have revealed that computer programs contain variables and statements. The fundamental building blocks of computer programs are variables, statements, and data structures. Once you know those fundamental building blocks, you can combine them in unique ways to express yourself creatively and to engage in problem solving. Note that variables contain information, which we generally refer to as data. Each variable contains one particular type of data. With knowledge of three data types and the skill to use three types of statements, three data structures, and one technique, you will be able to program a computer for general purpose problem solving and creative expression. 3.1.2 Three Data Types In JavaScript programming, a variable can be a number, a character string, or a Boolean value. A Boolean value is either true or false. The Boolean type is named after George Boole, an early nineteenth-century mathematician and philosopher, who developed Boolean algebra or Boolean logic. As an aside, Boolean algebra offers a set of 16 propositions based on two variables (x and y), each of which is either true or false...

  • Coding + Math
    eBook - ePub

    Coding + Math

    Strengthen K–5 Math Skills With Computer Science

    ...For example, the math operators +, -, x, and ÷ are represented in several programming languages as +, -, *, and /, respectively. Relational operators (e.g., <, >, =, >=, <=) are used in code in much the same way they are used in mathematics. Boolean logic operators (e.g., And, Or, Not) are evaluated in terms of being true or false, and they are useful both in decision trees and database searches. Programmers need to understand the difference between statements like “a And b”, “a Or b”, and “Not (a And b)”. Preparing learners for using such operators can be accomplished in mathematics by the use of word problems. For example, a young learner may be capable of understanding that the statement “It’s not going to rain and snow” can still be interpreted as it may rain, or it may snow, but both won’t happen. Representing this simple example as a “Not (a And b)” situation provides foundation for later understanding a Boolean expression in programming. Another type of operator that is instrumental in programming is an assignment operator, which is used to assign values to variable s in code. The foundation of the assignment operator can be found in the use of variables in mathematics when we say let “x = 5”. Even early in K–5 education, we learn to use this information to calculate “x + 2”. Assigning values to variables will be among the basic core building blocks of the beginning of coding in any programming language. Although there are many other operators available in various programming languages, those previously listed represent basic foundational operators that all programmers would be expected to acquire. Zhang and Nouri (2019) found mixed results among the eleven articles that examined the use of Boolean logic operators in Scratch. One study showed good improvement among Scratch users in understanding Boolean logic, whereas another showed only about half of the students examined able to answer questions using the OR operator...