JS
JS
1.Variables
- is a storage area it will hold the data
- in JS to define Variables we use let or Var
for eg : let a // declaring variable
a = 5 // initialize variable
invalid
- names cannot start with numbers.
for eg:
let 1a; // give you a error
JS is case sensitive
for eg :
let a = 5;
let A = "hi"; // here a and A are differnt Variables
console.log(a) // 5
console.log(A) // hi
2.Constant
- const introduced in EcmaScript6(ES6) to create Constant
for eg:
const a = 5;
const b = 6;
- Once constant is initialized, we cannot change the value.
for eg:
const a = 5;
a = 6; // throw an error
- then you cannot declare const without initialize
for eg:
const x;
x = 5; // this will throw an error.
3. console.log()
- syntax to print
for eg:
console.log("Hi")
console.log("I'm learning JS")
- to print value stored in variable
let a = 5;
let name = "Deepak"
console.log(a)
console.log(name)
4.Data Types
string
for eg :
Single quotes: 'Hello'
Double quotes: "Hello"
Backticks: `Hello` // used when you have to add expression into the
string
number
for eg:
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
BigInt
- number will have value upto (2^53 - 1) if you want to go more than that go
for BigInt
- BigInt number is created by appending n to the end of an integer
for eg:
const value = 900719925124740998n;
- we can add two bigint value but we cant add BigInt and number //
900719925124740998n + 1
Boolean
- it will have one of two values : true or false
for eg:
const doingIntern = true;
const gotFulltime = false;
undefined
- variable is declared but the value is not assigned //let a;
null
- represent empty or unknown value //let a = null;
-* null is not the same as NULL or Null.
Object
- it allows us to store collections of data
for eg:
const internEmp = {
firstName: 'Deepak',
lastName: null,
dept: 'R&D Intern'
};
5. Operators
- special symbol used to perform operations on values
differnt Operators
Assignment Operators =,+=,-=,*=,/=
Arithmetic Operators +,-,*,/,%
Comparison Operators >,>=,<,<=,===,!=
Logical Operators &&,||,!
Bitwise Operators >>,<<
Other Operators ?:
6. Comments
// - Single Line Comments
/* */ - Multi-line Comments
7.if-else
- three forms of if-else statement
if statement
if...else statement
if...else if...else statement
if statement
- If the condition is evaluated to true,body of if is executed
- If the condition is evaluated to false, body of if is skipped
for eg:
if (condition) {
// the body of if
}
if...else statement
if (condition) {
// block of code if condition is true
} else {
// block of code if condition is false
}
if (condition1) {
// code block 1
} else if (condition2){
// code block 2
} else {
// code block 3
}
}else{
}
}else{
8. for loop
for (initialExpression; condition; updateExpression) {
// for loop body
}
9. while loop
while (condition) {
// body of loop
}
break:
- used to terminate the loop
for eg:
for(){
if(){
break;
}
}
continue:
- used to skip the current iteration of the loop
for eg:
for(){
if(){
continue;
}
}
11. switch
- used in decision making
for eg:
switch(variable/expression) {
case value1:
// body of case 1
break;
case value2:
// body of case 2
break;
case valueN:
// body of case N
break;
default:
// body of default
}
II. Functions
1.Function
- block of code that performs a specific task
- dividing a complex problem into smaller piece
declaring a function
function nameOfFunction () {
// function body
}
for eg:
function with parameter
function addNumbers(a,b){
console.log(a + b)
}
addNumbers(5,4) // calling a function
Function return
- return statement used to return the value to the function call
- return statement --> function has ended. Any code after return will not
be executed
for eg:
function addNumbers(a,b){
return a+b;
}
addNumbers(5,4)
Benefits of Function
- reuse the code
- declare one time and use it for multiple times
2.Variable Scope
- it has two types
Global Scope - variable declare outside the function is global variable
Local Scope - variable declare inside the function is local variable
Global Scope
let a = 10; // Global Scope
function eg(){
console.log(a); // can be access inside
}
eg();
Local Scope
function eg(){
let a = 10 // local Scope
}
eg();
console.log(a); // cannot be access outside the function
3.Hoisting
- function or a variable can be used before declaration
-* keyword var allow hoisted and let and const does not allow hoisting
Variable Hoisting
for eg:
// the actuall Program // Program behave like
a = 5; var a;
console.log(a); ==> a = 5;
var a; // 5 console.log(a);
Function Hoisting
for eg:
greet(); // calling before declaring the function
function greet() {
console.log('Hi there');
}
4.Recursion
- function that calls itself (calling itself)
function recurse() {
recurse(); // calls itself
}
recurse(); // calling function
III.Objects
1.Object
- it allows you to store multiple collections of data
const Emp = {
name: 'Rahul', // key1 : value1
dept: 'R&D Intern' // key2 : value2
};
Nested object
for eg:
const Emp = {
name: 'Rahul',
age: 23,
Skills: {
tech: "python", // to access tech : Emp.Skills.tech
frontend: null, // to access frontend : Emp.Skills.frontend
backend: "MongoDB", // to access backend : Emp.Skills.backend
}
}
Object Methods
for eg:
const Emp = {
name: 'Rahul',
age: 23,
Skills: {
tech: "python", // to access tech : Emp.Skills.tech
frontend: null, // to access frontend :
Emp.Skills.frontend
backend: "MongoDB", // to access backend :
Emp.Skills.backend
}
greet: function(){
console.log("Hello")
}
}
2.Methods
- Adding a Method to a JavaScript Object
for eg:
let Emp = { };
Emp.name = 'srivarshan';
Emp.greet = function() {
console.log('hello' + ' ' + Emp.name);
}