Javascript Syntax something like C or Java language. Javascript is case sensitive, meaning that lowercase and uppercase letters are different.
Each line of javascript code separated the new line or it could be a semicolon (;)
Comments in javascript starting with / / or written between / * and *
Variable in javascript can be started with a letter or underscore (_) or a dollar sign ($).
Example: count_hits, _age
Declaration of variables :
- You can declare using var, example: var x = 5, this is a local and global (can be accessed by all functions)
- Or immediately declared without var, x = 5
| Operator | Definition | Examples |
| + | Addition | 5+4=9 |
| - | Subtraction | 4-3=1 |
| * | Multiplication | 3*5=15 |
| / | Division | |
| % | Modulus (division remainder) | 5%2=1 |
| ++ | Increment | |
| -- | Decrement |
Examples :
<script>
var x = 4;
var y = 2;
z = x + y;
alert(z);
</script>
Operator Assignment
Like most other programming languages, to give value to a variable using the equals sign =
Here is the brevity of operator writing :
| Shorthand Operator | Meaning |
| x += y | x = x + y |
| x -= y | x = x - y |
| x *= y | x = x * y |
| x /= y | x = x / y |
Example :
<script>
var x = 4;
var y = 2;
x -= y
alert(x);
</script>
Comparison Operators :
Useful to compare the value of a variable :
| Operator | Definition | Example |
| == | equal to | var1 == “Desrizal” |
| != | not equal | x != y |
| > | greater than | x > y |
| < | less than | x < 6 |
| >= | greater than or equal to | x>= y |
| <= | less than or equal to | x < 5 |
Example :
<html>
<head>
<script>
var x = 4;
var y = 2;
if(x > y){
alert("x greater then y");
}
</script>
</head>
<body>
</body>
</html>
Logical Operators :
| Operator | Description | Example |
| && | and | (x < 10 && y > 1) is true |
| || | or | (x==5 || y==5) is false |
| ! | not | !(x==y) is true |
Example :
<script>
var x = 76;
if(x >= 80){
alert("Value = A");
}else if(x >= 70 && x < 80){
alert("Value= B");
}else if(x >= 60 && x < 70){
alert("Value= C");
}else{
alert("Value= D");
}
</script>
Sign up here with your email
