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
Related key terms
6 Key excerpts on "Javascript Operators"
- 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 imageWhat 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
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 - ePub
- Rob Larsen(Author)
- 2013(Publication Date)
- Wrox(Publisher)
The symbol is used in an expression with either one or two values and performs a calculation on the values to generate a result. For example, here is an expression that uses the multiplication operator:var area = (width * height);An expression is just like a mathematical expression. The values are known as operands . Operators that require only one operand (or value) are sometimes referred to as unary operators , whereas those that require two values are sometimes called binary operators .The different types of operators you see in this section are- Arithmetic
- Assignment
- Comparison
- Logical
- String
Arithmetic Operators
Arithmetic operators perform arithmetic operations upon operands. (Note that in the examples in Table 10-2 , x = 10.)Table 10-2:JavaScript’s Arithmetic OperatorsAssignment Operators
The basic assignment operator is the equal sign, but do not take this to mean that it checks whether two values are equal. Rather, it’s used to assign a value to the variable on the left of the equal sign, as you saw in the previous section, which introduced variables.The basic assignment operator can be combined with several other operators to allow you to assign a value to a variable and perform an operation in one step. For example, look at the following statement where there is an assignment operator and an arithmetic operator:total = total - profit; This can be reduced to the following statement: total -= profit;Although it might not look like much, this kind of shorthand can save a lot of code if you have many calculations like this to perform (see Table 10-3 - eBook - ePub
- 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
- OperatorsNote: 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. - eBook - ePub
C Programming
Learn to Code
- Sisir Kumar Jena(Author)
- 2021(Publication Date)
- Chapman and Hall/CRC(Publisher)
6 Operators and ExpressionsDOI: 10.1201/9781003188254-66.1 Introduction
This chapter will discuss the two most essential parts of any programming language: operators and expressions. An operator is a symbol that tells the computer to perform specific mathematical or logical operations. Operators are used in programs to manipulate data and variables. C is very rich in built-in operators.An operator is a symbol that tells the computer to perform specific mathematical or logical operations.Operators can be either binary or unary. Binary operators are the operators where one operator will act upon two operands.For example:12 + 14In this example, the operator + is acted upon two operands 12 and 14. Hence + is a binary operator here. Similarly, a unary operator has one operator and one operand.For example:− 25In this example, the operator – is acted upon one operand 25. Hence – is a unary operator here. The following are the operators used in C. These operators may be used as binary or unary operators.- Arithmetic operators;
- Relational operators;
- Assignment operators;
- Logical operators;
- Increment or decrement operators;
- Conditional operators;
- Bitwise operators;
- Special operators.
In the subsequent section, we will discuss the feature of all these operators, how to form expressions, how they are executed, and in what order. Finally, we will introduce the precedence of operators.On completion of this chapter, students will have learnt the working of different operators, the precedence of operators, and the way the C compiler executes an expression. Some new operators like increment, decrement, ternary, and other special operators are also a part of this chapter. - eBook - ePub
- Matt Frisbie(Author)
- 2019(Publication Date)
- Wrox(Publisher)
The core language features of JavaScript are defined in ECMA-262 as a pseudolanguage named ECMAScript. ECMAScript contains all of the basic syntax, operators, data types, and objects necessary to complete basic computing tasks, though it provides no way to get input or to produce output. Understanding ECMAScript and its intricacies is vital to a complete understanding of JavaScript as implemented in web browsers. The following are some of the basic elements of ECMAScript:- The basic data types in ECMAScript are Undefined, Null, Boolean, Number, String, and Symbol.
- Unlike other languages, there's no separate data type for integers versus floating-point values; the Number type represents all numbers.
- There is also a complex data type, Object, that is the base type for all objects in the language.
- A strict mode places restrictions on certain error-prone parts of the language.
- ECMAScript provides a lot of the basic operators available in C and other C-like languages, including arithmetic operators, Boolean operators, relational operators, equality operators, and assignment operators.
- The language features flow-control statements borrowed heavily from other languages, such as the if statement, the for statement, and the switch statement.
- There is no need to specify the return value of the function because any function can return any value at any time.
- Functions that don't specify a return value actually return the special value undefined
- eBook - ePub
Multimedia Web Design and Development
Using Languages to Build Dynamic Web Pages
- Theodor Richardson, Charles Thies(Authors)
- 2012(Publication Date)
- Mercury Learning and Information(Publisher)
y is:Alternatively, you can combine these declarations into a single line by separating the variable names with a comma:6.1.2 Assigning ValuesWhen 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:
Learn about this page
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.