Here is the code: <script> var x = 33; var y = 10 + x; alert(y); </script> Expected Result: 43 Explanation: Variable x is assigned the value 33 Variable y performs the operation 10 + x (which is 33) The result 43 is displayed in an alert box
JavaScript Arithmetic Operators •Arithmetic operators are used to perform mathematical calculations on variables and/or values. These operators are combined with values and/or variables to form a JavaScript expression. • A symbol or other character indicating an operation that acts on one or more elements. • The different JavaScript arithmetic operators are listed as follows:
Web browser result:10 + y= 22 Explanation: var (means variable, optional) This are used to hold values and expressions. let variable y = 12. Mathematical expression= let variable x= 10 + y (where y is equal to 12) document .write display the result: 10 + y= 22
6.
<script> msg1= “My Birthdayis” msg2= “December 25, 1972” msg3= msg1 + msg2; alert(“ ”+msg3+””) </script> Web browser result: Explanation: The (+) operator add string variables or text values together.
7.
If you adda numeric value and a text or string value, the result will be a text or string Examples: Statement Result z= 10+5; 15 z= “3”+ “3”; 33 z= 8+ “15”; 815 z= “11”+12; 1112
8.
• <script> • z="13"+"13"; •x= 13+13; • document.write(""+z+""); • alert(""+x+""); • </script> • Result= 1313 (because values on z are considered as “text values”) • Result= 26(because values on x are numeric values)
9.
• <script> • varz= parseFloat("13"); • var x= 13 • var y= z + x; • document.write(""+y+""); • </script> RESULT= z + x = 26 • The parseFloat() function parses a string and returns a floating point number. • This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string. • Note: Only the first number in the string is returned! • Note: Leading and trailing spaces are allowed. • Note: If the first character cannot be converted to a number, parseFloat() returns NaN.
10.
COMPUQUIZ/EXERCISES 1. A symbolor other character indicating an operation that acts on one or more elements. 2. This are used to hold values and expressions. 3. Write the code for the following: variable x = 33 variable y= 10 + x Put the result in an alert box