Variables:
It is a container to store values or it is named memory location.
it is also called as identifier or reference variable.
It varies value so it is called variable.
a=10;
Rules for creating variable name:
1.Each variable has unique name and case sensitive.
2.It can start with letter, $ and _ and not with number.
- It should not use reserved words.
Naming convention:
camel case- variable name and function name follows- employeeId
Pascal case- class and Interfaces follows- Employee
under case- employee_Id
keywords which are used to declare variables:
- let _– when we assign the value to the variablei.e during declaration we give let a=10; re declaration is not allowed. Reassignment a=15 is allowed *_const _– re-declartion and reassignment both not allowed *_var – it is legacy keyword not recommended to use. It allows both re declaration and reassignment. var a=10; var a=15;
To see the output of the js ie. console use(ctrl+shift+i) or right click on the browser webpage and inspect
if we reassign or redeclare the const, we will get error
Printing multiple variable by comma separation:
let a=10;
let b=15;
we can print more that one variable by comma separation
console.log(a,b);
printing statements:
- alert(“hi”) or we can pass variable alert(a) – one button- ok
- confirm(“hi”) – same as alert but in confirm there will be 2 buttons- ok and cancel
- document.write() or writeln- prints in UI
- prompt(“enter the num”)- used to get input dynamically from user let num=prompt(“enter the num”) console.log(num)
different methods in console:
let a;
1.console.log(a)- undefined – variable is declared but initialized means we get undefined value.
2.console.error(a)-displays undefined as error
3.console.warn(a)-displays undefined as warning
4.console.clear()- clears all the printed msg
Operator:
It is used to perform operations.
Types of Operators:
1.Arithmetic operator
2.Comparison operator or Relational operator
3.Assignment operator
4.Logical operator
5.Bitwise operator
6.Ternary operator
7.unary operator
1.Arithmetic operator:
It is used to perform arithmetic operations.Operator:
It is used to perform operations.
- (addition)
- (subtraction)
- (multiplication) / (division) % (modulus - remainder of division) ** (exponentiation - ES2016) ++ (increment) -- (decrement)
-
Assignment operator:
Assign values to variables.= (assignment)
+= (addition assignment)
-= (subtraction assignment)
= (multiplication assignment)
/= (division assignment)
%= (modulus assignment)
*= (exponentiation assignment)
assigning right hand value to left hand variable.
+= -= = /= %= *=
assigning value to the same variable.
a=a+1
a+=1, a-=1
3.Comparison or relational operator:
compares 2 values and returns boolean value(true or false)
== (equality)
!= (inequality)
=== (strict equality - checks value and type)
!== (strict inequality)
(greater than)
< (less than)
= (greater than or equal to)
<= (less than or equal to)
4.Logical operator:
Combine or manipulate Boolean expressions.
&& (logical AND)
|| (logical OR)
! (logical NOT)
Concatenation (+)
Combining more than one string is called concatenation.
This is just like java concatenation.
Example:
let num=5;
let first=1;
console.log(num+”*”+first”=”num*first)
To ease the string concatenation template string is introduced since ES6 version.
Template string or Template literal:
Here we use `` backtick and to get the value of the variable inside backtick use ${}.