Tips n Tracks

  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • black color

Reference

Sample image

Microsoft .NET Framework Get Details.

Sample image

Microsoft .NET Framework Get Details.

Reference

Sample image Microsoft .NET Framework Get Details.
Sample image

Microsoft .NET Framework Get Details.


Learning JavaScript tutorial – JavaScript Expressions and Operators


Learning JavaScript tutorial – JavaScript Expressions and Operators

If you’re having problems with server load, maybe you should consider getting one of those dedicated servers.

JavaScript Expressions and Operators are very similar with most popular programming languages like C, C++, or Java. In this chapter we take an overview of the basic building blocks of JavaScript: operators, expressions.

JavaScript Expressions

JavaScript Expression is a combination of values, variables, operators, and functions that are interpreted (evaluated) as per the program logic define(develop) by JavaScript programmer. For example consider mathematical expression: 4+3 is an arithmetic and programming expression which evaluates to 7. The assignment expression x=4+3 also evaluates to 7 and assign that value to variable x. The expression is use to evaluate the the values for pre-define JavaScript program logic.

var intNum;  //Expression to declare a variable
var intNum=10; //Expression to declare a variable with assigned initial value
var x=5, y=4, z=x+y; //Expressions to declared multiple variables

JavaScript Operators

JavaScript supports a variety of operators like arithmetic, logical and comparison etc. An operator is a function which acts on any number of operands (inputs) to produce a pre-define operation.

JavaScript Assignment Operators

JavaScript Assignment operators are used to assign values to JavaScript variables. In many cases or instances JavaScript Assignment Operator is used to set a variable to a literal value, for example:

var studentName = "Max"; //studentName set to Max
var aNumber = 1982; //aNumber set to 1982
var isNumber = true; //isNumber set to true

Lets consider x=10 and y=5, the table below explains the advanced use of assignment operators:

Table 1 -JavaScript Assignment Operators
Operator Example Equivalent Result
= x=y   x=5
+= x+=y x=x+y x=15
-= x-=y x=x-y x=5
*= x*=y x=x*y x=50
/= x/=y x=x/y x=2
%= x%=y x=x%y x=0
<<= x <<= y x = x << y x=320
>>= x >>= y x = x >> y x=0
>>>= x >>>= y x = x >>> y x=0
&= x &= y x = x & y x=0
|= x |= y x = x | y x=15
^= x ^= y x = x ^ y x=15

JavaScript Arithmetic Operators

Arithmetic operators are used to perform all the basic arithmetic operations between operands (inputs) i.e. variables and/or values.

Table 2 – JavaScript Arithmetic Operators
Operator Meaning Example Result
+ Addition x=4+2 x=6
- Subtraction x=4-2 x=2
* Multiplication x=4*2x x=8
/ Division x=5/2 x=2.5
% Modulus (remainder) x=5%2 x=1
++ Increment x=++5 x=6
Decrement x=–5 x=4

JavaScript Comparison Operators

JavaScript Comparison operators are used in logical statements to determine equality or difference between variables or values. JavaScript comparison Operators evaluates to a Boolean value indicating whether its comparison is true or false. Most of JavaScript’s comparison operators should be familiar to elementary mathematics Comparison Operators.

Table 3 – JavaScript Comparison Operators
Operator Meaning Example Result
< Less than 5 < 8 true
<= Less than or equal to 8 <= 5 false
> Greater than 8 > 3 true
>= Greater than or equal to 5 >= 5 true
!= Not equal to 8 != 5 true
== Equal to 9 == 5 false
=== Equal to (identical) 5 === ’5′ false
!== Not equal to 5 !== ’5′ true

JavaScript Bitwise Shift Operators

JavaScript Bitwise Shift Operators are used to move (shift) bits around rather than set them. Bitwise shift operators take two operands. The first is the number to be shifted, and the second specifies the number of bit positions by which all the bits in the first operand are to be shifted. The direction of the shift operation is controlled by the operator used, << for left shift and >> for right shift.

Table 4 – JavaScript Bitwise Shift Operators
Operator Description Example Intermediate Step Result
<< Left shift 4<<3 00000100 shifted to the left three spots and filled with zeros results in 00100000. 32
>> Right shift with sign extend –9>>2 11110111 shifted to the right two spots and left-filled with the sign bit results in 11111101. –3
>>> Right shift with zero fill 32>>>3 00100000 shifted to the right three spots and left-filled with 0 results in 00000100. 4

JavaScript Logical Operators

JavaScript Logical operators are used to determine the logic between variables or values. The logical operators && (AND), || (OR), and ! (NOT) are useful to combine such values together in order to implement more complicated logic. The most common use of the logical operators is to control the flow of script execution using conditional statement.

Table 5 – JavaScript Logical Operators
&& true if both operands evaluate true; otherwise returns false. var x=true, y=false; alert(x && y); // displays false
|| Returns true if either operand is true. If both are false, returns false. var x=true, y=false; alert(x || y); // displays true
! If its single operand is true, returns false; otherwise returns true. var x=true; alert(!x); // displays false

JavaScript Conditional Operator

JavaScript Conditional Operator evaluate eventually a condition to true or false. If expression evaluates true, if-true-statement is evaluated. Otherwise, if-false-statement is executed.

The basic syntax for JavaScript Conditional Operator:

(expression) ? if-true-statement : if-false-statement;
var x=7;
(x >> 5) ? alert("x is greater than 5") : alert("x is less than 5");

JavaScript Comma Operator

The comma operator (,) allows multiple expressions to be strung together and treated as one expression. Expressions strung together with commas evaluate to the value of the right-most expression. JavaScript Comma Operator mostly use with for condition.

var a,b,c,d;
for(i=0; i<=10; i++)
{
//Some JavaScript Statements
}

JavaScript void Operator

The void operator specifies an expression to be evaluated without returning a value.

var a=0;
a = void (d=56);
alert(a); // the value of ‘a’ will alert as ‘undefined’

JavaScript typeof Operator

The typeof operator returns a string indicating the data type of its operand.

var a=3
var name = "I Love JavaScript";
alert(typeof a);       // alert ‘number’
alert(typeof name);    // alert ‘string’

Table 6 – JavaScript typeof Operator Return Values
Type String Returned by typeof
Boolean "boolean"
Number "number"
String "string"
Object "object"
Function "function"
Undefined "undefined"
Null "object"

 

Chetan love blogging. He regularly blogs at http://www.tipsntracks.com. You can connect with Chetan on Twitter, Facebook and Google Plus...

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

[...] our last article "Learning JavaScript tutorial – JavaScript Expressions and Operators", we take a look at JavaScript Operators. JavaScript Operators have a predefined order of [...]

Pingback by Learning JavaScript tutorial - JavaScript Operator Precedence and Associativity | Tips n Tracks on April 14, 2010 @ 2:17 pm

Leave a comment

(required)

(required)

*
To prove that you're not a bot, enter this code
Anti-Spam Image