JAVASCRIPT - OPERATORS

Similar documents
Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

JAVA OPERATORS GENERAL

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Operators. Java operators are classified into three categories:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

A flow chart is a graphical or symbolic representation of a process.

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

Operators in C. Staff Incharge: S.Sasirekha

SECTION II: LANGUAGE BASICS

Informatics Ingeniería en Electrónica y Automática Industrial

CGS 3066: Spring 2015 JavaScript Reference

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Unit-2 (Operators) ANAND KR.SRIVASTAVA

About the Tutorial. Audience. Prerequisites. Copyright and Disclaimer

CT 229. Java Syntax 26/09/2006 CT229

Prof. Navrati Saxena TA: Rochak Sachan

Operators & Expressions

Stitch Language Reference Manual

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Expression and Operator

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Chapter 3: Operators, Expressions and Type Conversion

Chapter 4. Operations on Data

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

Expressions and Precedence. Last updated 12/10/18

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

The Java language has a wide variety of modifiers, including the following:

QUIZ: What value is stored in a after this

Client-Side Web Technologies. JavaScript Part I

CSC Web Programming. Introduction to JavaScript

Department of Computer Science

Unit 3. Operators. School of Science and Technology INTRODUCTION

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Information Science 1

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Variables and Operators 2/20/01 Lecture #

Outcomes Week 2 Overview Describe the inputs, activities, and outputs of each step in the software development life cycle. Describe arithmetic, relati

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

JavaScript CS 4640 Programming Languages for Web Applications

Chapter 4: Basic C Operators

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Chapter 2 Working with Data Types and Operators

ECEN 468 Advanced Logic Design

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Computers Programming Course 6. Iulian Năstac

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Arithmetic and Bitwise Operations on Binary Data

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Here n is a variable name. The value of that variable is 176.

Arithmetic and Bitwise Operations on Binary Data

Fundamentals of Programming CS-110. Lecture 3

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

ISA 563 : Fundamentals of Systems Programming

Chap 6 - Introduction to HDL (b)

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

LECTURE 3 C++ Basics Part 2

Data Handing in Python

x= suppose we want to calculate these large values 1) x= ) x= ) x=3 100 * ) x= ) 7) x=100!

Operators in java Operator operands.

Programming in C++ 5. Integral data types

Logical and Bitwise Expressions

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

JavaScript CS 4640 Programming Languages for Web Applications

Prepared by: Shraddha Modi

Lecture 3 Operators MIT AITI

CSC 1214: Object-Oriented Programming

PHPoC. PHPoC vs PHP. Version 1.1. Sollae Systems Co., Ttd. PHPoC Forum: Homepage:

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

JAVA Programming Fundamentals

Accumulator and memory instructions 1. Loads, stores, and transfers 2. Arithmetic operations 3. Multiply and divide 4. Logical operations 5. Data test

Constants and Variables

Lecture 3. More About C

GAS Tutorial - 6. Expression

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Here redirection. Case statement. Advanced Unix Tools Lecture 6 CS214 Spring 2004 Friday March 5, 2004

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

C/C++ Programming for Engineers: Working with Integer Variables

.Net Technologies. Components of.net Framework

Arithmetic Operators. Portability: Printing Numbers

GO MOCK TEST GO MOCK TEST I

Homework #3 CS2255 Fall 2012

CHAPTER 5: JavaScript Basics 99

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

UNIT- 3 Introduction to C++

BBM#101# #Introduc/on#to# Programming#I# Fall$2014,$Lecture$4$

DEPARTMENT OF MATHS, MJ COLLEGE

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Embedded Systems. Introduction. The C Language. Introduction. Why C instead ASM. Introduction to C Embedded Programming language

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

PHPoC vs PHP > Overview. Overview

Transcription:

JAVASCRIPT - OPERATORS http://www.tutorialspoint.com/javascript/javascript_operators.htm Copyright tutorialspoint.com What is an operator? Let us take a simple expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called the operator. JavaScript supports the following types of operators. Arithmetic Operators Comparision Operators Logical orrelational Operators Assignment Operators Conditional orternary Operators Lets have a look on all operators one by one. Arithmetic Operators JavaScript supports the following arithmetic operators Assume variable A holds 0 and variable B holds 20, then + Addition Adds two operands Ex: A + B will give 30 2 - Subtraction Subtracts the second operand from the first Ex: A - B will give -0 3 * Multiplication Multiply both operands Ex: A * B will give 200 4 / Division Divide the numerator by the denominator Ex: B / A will give 2 5 % Modulus Outputs the remainder of an integer division

Ex: B % A will give 0 6 ++ Increment Increases an integer value by one Ex: A++ will give 7 -- Decrement Decreases an integer value by one Ex: A-- will give 9 Note Addition operator + works for Numeric as well as Strings. e.g. "a" + 0 will give "a0". The following code shows how to use arithmetic operators in JavaScript. var a = 33; var b = 0; var c = "Test"; document.write("a + b = "); result = a + b; document.write("a - b = "); result = a - b; document.write("a / b = "); result = a / b; document.write("a % b = "); result = a % b; document.write("a + b + c = "); result = a + b + c; a = a++; document.write("a++ = "); result = a++; b = b--;

document.write("b-- = "); result = b--; Set the variables to different values and then try... Output a + b = 43 a - b = 23 a / b = 3.3 a % b = 3 a + b + c = 43Test a++ = 33 b-- = 0 Set the variables to different values and then try... Comparison Operators JavaScript supports the following comparison operators Assume variable A holds 0 and variable B holds 20, then = = Equal Checks if the value of two operands are equal or not, if yes, then the condition becomes true. Ex: A == B is not true. 2!= NotEqual Checks if the value of two operands are equal or not, if the values are not equal, then the condition becomes true. Ex: A! = B is true. 3 > Greaterthan Checks if the value of the left operand is greater than the value of the right operand, if yes, then the condition becomes true. Ex: A > B is not true. 4 < Lessthan Checks if the value of the left operand is less than the value of the right operand, if yes, then the condition becomes true. Ex: A < B is true.

5 >= GreaterthanorEqualto Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes, then the condition becomes true. Ex: A >= B is not true. 6 <= LessthanorEqualto Checks if the value of the left operand is less than or equal to the value of the right operand, if yes, then the condition becomes true. Ex: A <= B is true. The following code shows how to use comparison operators in JavaScript. var a = 0; var b = 20; document.write("(a == b) => "); result = (a == b); document.write("(a < b) => "); result = (a < b); document.write("(a > b) => "); result = (a > b); document.write("(a!= b) => "); result = (a!= b); document.write("(a >= b) => "); result = (a >= b); document.write("(a <= b) => "); result = (a <= b);

Output (a == b) => false (a < b) => true (a > b) => false (a!= b) => true (a >= b) => false a <= b) => true Logical Operators JavaScript supports the following logical operators Assume variable A holds 0 and variable B holds 20, then && LogicalAND If both the operands are non-zero, then the condition becomes true. Ex: A && B is true. 2 LogicalOR If any of the two operands are non-zero, then the condition becomes true. Ex: A B is true. 3! LogicalNOT Reverses the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false. Ex:! A && B is false. Try the following code to learn how to implement Logical Operators in JavaScript. var a = true; var b = false; document.write("(a && b) => "); result = (a && b); document.write("(a b) => "); result = (a b);

document.write("!(a && b) => "); result = (!(a && b)); <p>set the variables to different values and different operators and then try...</p> Output (a && b) => false (a b) => true!(a && b) => true Bitwise Operators JavaScript supports the following bitwise operators Assume variable A holds 2 and variable B holds 3, then & BitwiseAND It performs a Boolean AND operation on each bit of its integer arguments. Ex: A & B is 2. 2 BitWiseOR It performs a Boolean OR operation on each bit of its integer arguments. Ex: A B is 3. 3 ^ BitwiseXOR It performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. Ex: A B is. 4 ~ BitwiseNot It is a unary operator and operates by reversing all the bits in the operand. Ex: B is -4. 5 << LeftShift It moves all the bits in its first operand to the left by the number of places specified in

the second operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on. Ex: A << is 4. 6 >> RightShift Binary Right Shift Operator. The left operand s value is moved right by the number of bits specified by the right operand. Ex: A >> is. 7 >>> RightshiftwithZero This operator is just like the >> operator, except that the bits shifted in on the left are always zero. Ex: A >>> is. Try the following code to implement Bitwise operator in JavaScript. var a = 2; // Bit presentation 0 var b = 3; // Bit presentation document.write("(a & b) => "); result = (a & b); document.write("(a b) => "); result = (a b); document.write("(a ^ b) => "); result = (a ^ b); document.write("(~b) => "); result = (~b); document.write("(a << b) => "); result = (a << b); document.write("(a >> b) => "); result = (a >> b);

<p>set the variables to different values and different operators and then try...</p> (a & b) => 2 (a b) => 3 (a ^ b) => (~b) => -4 (a << b) => 6 (a >> b) => 0 Assignment Operators JavaScript supports the following assignment operators = SimpleAssignment Assigns values from the right side operand to the left side operand Ex: C = A + B will assign the value of A + B into C 2 += AddandAssignment It adds the right operand to the left operand and assigns the result to the left operand. Ex: C += A is equivalent to C = C + A 3 = SubtractandAssignment It subtracts the right operand from the left operand and assigns the result to the left operand. Ex: C -= A is equivalent to C = C - A 4 *= MultiplyandAssignment It multiplies the right operand with the left operand and assigns the result to the left operand. Ex: C *= A is equivalent to C = C * A 5 /= DivideandAssignment It divides the left operand with the right operand and assigns the result to the left operand. Ex: C /= A is equivalent to C = C / A 6 %= ModulesandAssignment

It takes modulus using two operands and assigns the result to the left operand. Ex: C %= A is equivalent to C = C % A Note Same logic applies to Bitwise operators so they will become like <<=, >>=, >>=, &=, = and ^=. Try the following code to implement assignment operator in JavaScript. var a = 33; var b = 0; document.write("value of a => (a = b) => "); result = (a = b); document.write("value of a => (a += b) => "); result = (a += b); document.write("value of a => (a -= b) => "); result = (a -= b); document.write("value of a => (a * = b) => "); result = (a *= b); document.write("value of a => (a /= b) => "); result = (a /= b); document.write("value of a => (a %= b) => "); result = (a %= b); <p>set the variables to different values and different operators and then try...</p> Output Value of a => (a = b) => 0 Value of a => (a += b) => 20 Value of a => (a -= b) => 0 Value of a => (a *= b) => 00 Value of a => (a /= b) => 0 Value of a => (a %= b) => 0

Miscellaneous Operator We will discuss two operators here that are quite useful in JavaScript: the conditional operator? : and the typeof operator. Conditional Operator? : The conditional operator first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation.? : Conditional If Condition is true? Then value X : Otherwise value Y Try the following code to understand how the Conditional Operator works in JavaScript. var a = 0; var b = 20; document.write ("((a > b)? 00 : 200) => "); result = (a > b)? 00 : 200; document.write ("((a < b)? 00 : 200) => "); result = (a < b)? 00 : 200; <p>set the variables to different values and different operators and then try...</p> Output ((a > b)? 00 : 200) => 200 ((a < b)? 00 : 200) => 00 typeof Operator The typeof operator is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand. The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean value and returns true or false based on the evaluation.

Here is a list of the return values for the typeof Operator. Type Number String Boolean Object Function Undefined Null String Returned by typeof "number" "string" "boolean" "object" "function" "undefined" "object" The following code shows how to implement typeof operator. var a = 0; var b = "String"; result = (typeof b == "string"? "B is String" : "B is Numeric"); document.write("result => "); result = (typeof a == "string"? "A is String" : "A is Numeric"); document.write("result => "); <p>set the variables to different values and different operators and then try...</p> Output Result => B is String Result => A is Numeric Processing math: 00%