Java script operators

    Operators are used to perform mathematical, logical operations on variables. In javascript,it has many categories.

They are listed below.

  • 1.       Arithmetic operators
  • 2.       Logical operators
  • 3.       Bitwise operators
  • 4.       comparison operators
  • 5.       Assignment operators
  • 6.       Others.

 

1.       1. Arithmetic operators:

    This is basic operator  which is used to perform arithmetic operations like addition, subtraction and so on.

Operator

Meaning

Usage

+

Addition

a+b ,a+50

-

Subtraction

a-b, a-10

*

Multiplication

a*b

/

Division

a/b

%

Modulation

a%b

 2.       logical operators:

    Logical operators involve with three different types of logic. It checks between two variables by three conditions.

Operator

Meaning

Usage

&&

Logical AND

a &&b

||

Logical OR

a|| b

!

Logical NOT

!a

3.      Bitwise operators:

If you want to perform bitwise operations on variables, use the below operators.

Bitwise Operators

Meaning

Usage

&

Bitwise AND

a &b

|

Bitwise OR

a| b

^

Bitwise XOR

a^b

~

Bitwise NOT

~a

<< 

Left shift

a<<2

>> 

Right shift

a>>2

>>> 

Right shift with zero

a>>>2


4.     4.  Comparison Operators:

When you want to compare the values of the variables,use these operators.

Operator

Meaning

Usage

> 

Greater than

a>b

< 

Less than

a<b

>=

Greater than or equal to

a>=b

<=

Less than or equal to

a<=b

==

Equal to

a==b

!=

Not equal to

a!=b

 

5.Assignment operators:

        Sometimes,we need to assign values to variables. These variables are called assignment operators.

 

Operator

Meaning

Usage

=

Equal

A=10

+=

Add and assign the value

a+=14

-=

Subtract and assign the value

a-=5

*=

Multiply and assign the value

a*=4

/=

Divide and assign the value

a/=56

%=

Modulo operation and assign the value

a%=6

 6.Others:

   As java is a object orient programming language, it deals with some more special operators listed below.

Operator

Meaning

(?=)

Conditional operator

,

Comma operator

Delete

Removes a property of an object

In

Checks an object’s property

Instanceof

Finds the instance of the object

typeof

Gives the type of object

New

Creates an object

Yield

It gives the generator’s value

Void

Nothing is returned as output


These are the operators used in java script. 

Datatypes and Variables in java script

Datatypes are used to hold the values of variables. JavaScript has two types of datatypes.

·       Primitive data types

·       Non-primitive data types

Java script is a dynamic type. It is not important to mention the datatype earlier. Use “var” for variable.

Primitive data types:

Javascript has 5 primitive data types. They are listed below.

Boolean: it represents zero or one value.

Number: it represents numbers.

String :it represents sequence of characters.

Null : it represents no value.

Undefined: if you want to define the value later, use this data type.

Non- primitive data types:

Javascript has 3 different types of non-primitive data types. They are given below.

Array: It has a list of similar values

Object: it is a instance of a class.

Regexp: It is for Regular Expressions.

 

Variables:

 Variables are so important for any language. It specifies the value. In javascript, a key word ”var” is used.

Syntax:

var variablename=value;

Eg:

//assigning numbers

var a=10;

//assigning string

var name=”Jey”;

note: variable name should start with alphabets(lowercase or uppercase), underscore(_),dollar($). These variables are case sensitive.

Variables can be categorized into two. They are

Local variables: Variable’s scope is within the block.

Global variables: Variable’s scope is for the entire module.

Examples are shown as below…

Addition of three numbers in javascript(Eg: local variable)

<!DOCTYPE html>

<html>

<head> </head>

<body>

<script language="javascript">

var a=10; // local variable

var b=3; // local variable

var c=5; // local variable

var d=a+b+c;

document.writeln("The sum is:"+d);

</script>

</body>

</html>

Output:

 


Pop up message display in javascript (Eg: Global variable)

<html>

<body>

<script> 

var value="good vibes";//global variable 

function a1(){ 

alert(value); 

} 

a1();

</script> 

</body>

</html>

Output:

 


 Thus,the Data types and variables used in java script is explained with examples.

Different types of messages displaying in java Script -Full stack development

  Html uses script tag to include java script coding. The user can include the scripting in any part of html. It can be in <head> tag part , <body> tag part or as a external file.

Here, three different examples for message displaying are given.

àDisplaying a simple message

  • ·        Create a file in notepad and name it as “Message.html”
  • ·       Next, Type the following code.

<!DOCTYPE html>

<html>

<head> <title> Simple message</Title> </head>

<body>

<script language="javascript" >

document.write("Have a pleasant Day")

</script>

</body>

</html>

  • ·       Open the file in your web browser.
  • ·       It shows the output.

 

è àDisplay birthday message using command button

    Here, the java script includes an event handler.

  • Now, open new file in a notepad editor.
  •  Create the program using below code.

        <!DOCTYPE html>

        <html>

        <head>

        <script language="javascript" >

         <!--

            function birthdayWishes() {

               alert("Many more happy returns of the day")

            }

         //-->

         </script>

         </head>

        <body>

         <script type = "javascript">

         <!--

            document.write("Many more happy returns of the day")

         //-->

          </script>

           < input type = "button" onclick = "birthdayWishes()" value = "birthdayWishes" />

           </body>

            </html>

  •     Next, open your file “birthdayWishes.html” in your web browser.

 

è Scripting from External java script file

The above two examples use java script coding internally within the file. When you include the java script as a external file, here is the example.

Steps are given below.

·       Create 2 text files. Name them “source.html” and “sample.js” where “source.html” is the html file and “sample.js” is the external java script file.

·       In “source.html” ,add the below code.

<html>

   <head>

      <script type = "javascript" src = "c:\raji\sample.js" ></script>

   </head>

      <body>

      This is the source program

      <p onclick="sayHi()">Hi world</p>

   </body>

</html>

·       In “ sample.js” file, include the java script code.

function sayHi() {

   alert("Hi world")

}

·       The output is given below.

 


These are some basic java script programs. Try it by yourself.