Skip to main content

How to write Javascript?



Writing Comments in javaScript
JavaScript comments can be used to make the code more readable. JavaScript Comments can be added to explain the JavaScript, or to make it more readable.
You can use single line comment as well as multiple line comments:

JavaScript Single Line Comment: Single line comment start with //.

Check out the single lime comment written in following example.


JavaScript Multi-Line Comments: Multi line comments start with /* and end with */. This example uses a multi line comment to explain the code:






JavaScript Operators and Expressions

As any other programming language JavaScript has a set of operators that can be used to manipulate data. An operator transforms one or more value into resultant value. The value to which operator is applied is called operands. Operators and its operands are combined to form expression. Expressions can be evaluated to determine value of expression. The set of operators have been discussed below:

1. Arithmetic Operators: Arithmetic operators are the most familiar operators. They are used for common math calculations. Following are the arithmetic operators supported by javaScript.


Operator Description Expression Evaluating Expression for v=2
+ Addition u=v+4 u=6
- Subtraction u=v-1 u=1
* Multiplication u=v*6 u=12
/ Division u=v/2 u=1
% Modulus u=v%2 u=0
++ Increment u=++v u=4
-- Decrement u=--v u=0

The operator = is used to assign values, operator + is used to add values, assignment operator = is used to assign values to JavaScript variables and the arithmetic operator + is used to add values together.
E.g. Adding two numbers using a arithmetic operators
var a=1;
var b=2;
c=a+b;
document.write (c);
The output of the above program will be 3.
An operator having single operand is referred as a Unary Operator and the requiring more than one operator is called a binary operator. The standard arithmetic operators like +,-,*, / in the above table are the binary operators. Both the increment and the decrement operator presented in the above table are the unary operators.
Using Increment/decrement Operators
Increment (++) and Decrement operator (--) can be used in two different ways. They can be placed before the operand and after the operand but the result in both the cases will be different. When the operator is placed before the operand like ++u then the value of the operand will be incremented by 1 and then the result will be output to the user. Check out the following example:
E.g. Using the increment operator before the operand
x=5;
y=++x;
document.write(y);
Output: 6
E.g. Using the decrement operator before the operand

x=4;
y=--x;
document.write(y);

Output: 3

u=4;
v=++u;
w=--u;
document.write(v);
document.write(w);

Output: 5 3

Placing the operator after the operand returns the value of the variable before incrementing or decrementing its value. Check out the following example:

E.g.

u=4;
v=u++;
w=u--;
document.write(v);
document.write(w);

Output:4 5

In the above example v is assigned the value of u (v=4) before u gets incremented by 1. v is then assigned value of u (v=5) and then u gets decremented by 1.

2. JavaScript Assignment Operators: Assignment operators are used to assign values to JavaScript variables. Some of the assignment operators facilitate manipulation and assignment of values at the same time. E.g. u=u+v can be done as u+=v. Following are the operators that are supported in JavaScript:
Operator Explanation Expression Evaluating
for u=4,v=2
= Set variables on left of the operator to value of expression placed on right u=v u=4
+= Incrementing the variable on the left of += operator by value of expression on its right. When used with string value of right appended to value of variable on left hand side. u+=v u=6
-= Decrementing variable on left side by value of expression on right u-=v u=2
*= Multiplies variable on left side by value of expression on right u*=v u=8
/= Divides the Variable on left side by value of expression on right u/=v u=2
%= Takes modulus of the variable on left side by value of expression on right u%=v u=0

Assignment operators facilitate tasks which can be done only by using multiple operators. This has been summarized in the following table:
Operator Expression providing similar results
+= u=u+v
-= u=u-v
*= u=u*v
/= u=u/v
%= u=u%v

3. Logical Operators: Logical operators are used to perform Boolean operations like and, or, not. They are used to determine the logic between variables or values. The logical operators supported by javaScript are:

Operator Description Expression Evaluating Expression
for a=3,b=4
&& and (a <5 && b > 1) True
|| or (a==4 || b==4) False
! not !(a==b) False
4. Comparison Operators: Comparison operators are used in logical statements to determine equality or difference between variables or values. Comparison and Logical operators are used to test for true or false. Following are the comparison operators supported by JavaScript.
Operator Description Expression Evaluating Expression
for u=5
== is equal to u==7 False
=== is exactly equal to (value and type) U===5
u==="5" u===5 is true
u==="5" is false
!= is not equal u!=6 True
> is greater than u>7 False
< is less than u<7 True >= is greater than or equal to u>=7 False
<= is less than or equal to U<=7 True

The equal (==”) and not equal (!=) operators perform type conversion before testing for equality e.g. “3”==3 evaluates to be true as the string “3” is converted to an integral value 3. On the other hand strictly equal (= = =) and the strictly not equal (!===) do not perform any type conversion before testing for equality. E.g. “3”===3 will return false as data types are different.


Note: Comparison operators can be used in conditional statements to compare values and take action depending on the result:

if (age<18) document.write("Not eligible to vote");

Comments

Popular posts from this blog

Advantages and Disadvantages of EIS Advantages of EIS Easy for upper-level executives to use, extensive computer experience is not required in operations Provides timely delivery of company summary information Information that is provided is better understood Filters data for management Improves to tracking information Offers efficiency to decision makers Disadvantages of EIS System dependent Limited functionality, by design Information overload for some managers Benefits hard to quantify High implementation costs System may become slow, large, and hard to manage Need good internal processes for data management May lead to less reliable and less secure data

Inter-Organizational Value Chain

The value chain of   a company is part of over all value chain. The over all competitive advantage of an organization is not just dependent on the quality and efficiency of the company and quality of products but also upon the that of its suppliers and wholesalers and retailers it may use. The analysis of overall supply chain is called the value system. Different parts of the value chain 1.  Supplier     2.  Firm       3.   Channel 4 .   Buyer

Big-M Method and Two-Phase Method

Big-M Method The Big-M method of handling instances with artificial  variables is the “commonsense approach”. Essentially, the notion is to make the artificial variables, through their coefficients in the objective function, so costly or unprofitable that any feasible solution to the real problem would be preferred, unless the original instance possessed no feasible solutions at all. But this means that we need to assign, in the objective function, coefficients to the artificial variables that are either very small (maximization problem) or very large (minimization problem); whatever this value,let us call it Big M . In fact, this notion is an old trick in optimization in general; we  simply associate a penalty value with variables that we do not want to be part of an ultimate solution(unless such an outcome is unavoidable). Indeed, the penalty is so costly that unless any of the  respective variables' inclusion is warranted algorithmically, such variables will ...