We will learn about
- Display Output
- Data Types
- Comments
- Variables
- Operators and its types
First JS program
“console.log” is used to log (print) something on the console(screen).
console.log("Hello World!");
Output:
Hello World!
Any thing written inside the Quotation Mark (“”) will be displayed on screen.
Data Types in JS
We store different types of data.
- Numbers, e.g. 123, 120.50 etc.
- Strings of text e.g. “This text string”, name etc.
- Boolean e.g. true or false.
- There are other data types which we will discuss later like null, undefined, symbol etc
Comments
Single Line Comments
Single line comments start with //
.
Any text between //
and the end of the line will be ignored by JavaScript (will not be executed).
Multi-line Comments
Multi-line comments start with /*
and end with */
.
Any text between /*
and */
will be ignored by JavaScript.
Variables
Variables are containers that store data. Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves.
JavaScript Variables can be declared in 4 ways:
- Automatically
var
: Variables can be redeclared and updated. A global scope variable.let
: Variables can not be redeclared but can be updated. A block scope variable.const
: Variables can not be redeclared and updated. A block scope variable.
var is used rarely. We use let and const to declare variables.
Variable Rules:
- Variable names are case sensitive. “a” and “A” is different.
- Variable names must start with a letter, an underscore (
_
) or a dollar sign ($
). - Variable names cannot contain spaces.
- Variables cannot be the same as reserved keywords such as
if
orconst
. - Variables should be given descriptive names that indicate their content and usage (e.g.
sellingPrice
andcostPrice
rather thanx
andy
). - By convention, JavaScript variable names are written in camelCase.
Code:
let age = 24; // number
console.log(age);
let name = "tony stark"; // string
console.log(name);
let isEmpty = true; // boolean
console.log(isEmpty);
const PI = 3.14;
console.log(PI);
Output:
24
tony stark
true
3.14
Operators
Operators are used to perform operations on data stored in variables.
JavaScript supports the following types of operators.
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
Arithmetic Operators
Common mathematical operations
Operator | Description |
---|---|
+ | Addition |
– | Subtraction |
* | Multiplication |
** | Exponentiation (ES2016) |
/ | Division |
% | Modulus (Division Remainder) |
Code:
let a = 5;
let b = 3;
let c = a + b;
let d = a - b;
console.log("a + b = ", c);
console.log("a - b = ", d);
console.log("a * b = ", a*b);
console.log("a / b = ", a/b);
console.log("a % b = ", a%b); // gives reminder
console.log("a ** b = ", a**b); // 5^2
Output:
a + b = 8
a - b = 2
a * b = 15
a / b = 1.6666666666666667
a % b = 2
a ** b = 125
Unary Operators
Unary operator requires only one operand to work.
Operator | Description |
---|---|
++ | increment (value is incremented by 1) |
— | decrement (value is decremented by 1) |
Code:
let x = 5 , y = 5;
x = x + 1;
console.log("x = ", x);
y++; // post increment: same as y = y + 1
console.log("y = ", y);
--y; // pre increment: same as y--
console.log( "y = " , y);
Output:
x = 6
y = 6
y = 5
Assignment Operators
Assignment operators assign values to JavaScript variables.
Operator | Example | Same As |
---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x – y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
**= | x **= y | x = x ** y |
Code:
let a = 5, b = 2;
a += b;
console.log("a = ", a);
b **= b; //2^2
console.log("b = ", b);
Output:
a = 7
b = 4
Comparison Operators
Compare values and return true or false
Operator | Description |
---|---|
== | equal to |
=== | equal value and equal type |
!= | not equal |
!== | not equal value or not equal type |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
Code:
let a = 5, b = 2; //numbers
let c = "5"; //string
console.log("a == b ", a==b);
console.log("a == c ", a==c);
console.log("a === c ", a===c);
Output:
a == b false
a == c true
a === c false
Logical Operators
&& LOGICAL AND returns true if both statements are true
|| LOGICAL OR returns true if one of the statements is true
! LOGICAL NOT reverse the result, returns false if the result is true
Code:
let x = 6;
let cond1 = x > 5; //true
let cond2 = x === 6; //true
console.log("cond1 && cond2", cond1 && cond2);
console.log((x < 5 && x < 10));
console.log((x < 4 || x < 10) );
console.log(!(x < 5 && x < 10));
Output:
cond1 && cond2 true
false
true
true
Leave a Reply