0% found this document useful (0 votes)
22 views8 pages

JS

The document provides an overview of JavaScript basics, including variable declaration, data types, operators, control flow structures, functions, and objects. It explains the differences between 'var' and 'let', introduces constants with 'const', and describes various data types like strings, numbers, and objects. Additionally, it covers variable scope, hoisting, recursion, and methods for accessing and manipulating object properties.

Uploaded by

Vishal 77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

JS

The document provides an overview of JavaScript basics, including variable declaration, data types, operators, control flow structures, functions, and objects. It explains the differences between 'var' and 'let', introduces constants with 'const', and describes various data types like strings, numbers, and objects. Additionally, it covers variable scope, hoisting, recursion, and methods for accessing and manipulating object properties.

Uploaded by

Vishal 77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

JavaScript

-Programming language for the web application

I.Basics and Control_Flow

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

let a = 10; //declaring and initialize variable in single line


var b = 20;

- let a; // variable without initializing it, it will have an undefined value

difference Var and let


Var
- var is used in older version JS
- var is function scoped
- var allows to redeclare variables.
- Hoisting occurs in var.
let
- let is block-scoped.
- let does not allow to redeclare variables.
- Hoisting does not occur in let.

variable naming rules in JS


valid
- name must be start with either letters or _ underscore or $ dollar.
for eg :
let a ;
let _a;
let $a;

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

Keywords cannot be used as variable names


for eg :
let const = 4;
let new = 5; // invalid

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 - combination of characters //'hello', "hello


world!"
number - integer or floating point number //3, 3.234
boolean - either true or false //true and false
BigInt - deals with big numbers
//900719925124740999n , 1n
undefined - a data type whose variable is not initialized //let a;
null - denotes null value //let a = null;
Symbol - data type whose instances are unique and immutable //let value =
Symbol('hello');
Object - key-value pairs of collection of data //let data = { };

string
for eg :
Single quotes: 'Hello'
Double quotes: "Hello"
Backticks: `Hello` // used when you have to add expression into the
string

const name = 'rahul';


const name1 = "srivarshan";
const result = `The names are ${name} and ${name1}`;

number
for eg:
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5

- in number we have +infinity, -infinity and Nan(not an number)


for eg:
const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;


console.log(number2); // -Infinity
const number3 = "abc"/3; // strings can't be divided by numbers
console.log(number3); // NaN

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...else if...else statement


- If condition1 is true, the code block 1 is executed.
- If condition1 evaluates to false, then it will move to condition2 .
- If the condition2 is true, the code block 2 is executed.
- If the condition2 is false, the code block 3 is executed.

if (condition1) {
// code block 1
} else if (condition2){
// code block 2
} else {
// code block 3
}

nested if else statement


- we can use if-else inside if else statement
for eg:
if(){
if(){

}else{

}
}else{

8. for loop
for (initialExpression; condition; updateExpression) {
// for loop body
}

9. while loop
while (condition) {
// body of loop
}

10. do.. while loop


do {
// body of loop
} while(condition)

when to use for and while loop


- for loop is used when the number of iterations is known.
- while and do.. while used when the number of iterations are unknown

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

- when local variable is used without declaring it, then that


variable automatically becomes a global variable.
for eg:
function eg(){
a = 10 // local Scope
}
eg();
console.log(a);

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);

- initialization are not hoisted


for eg:
console.log(a);
var a = 10;

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
};

const Emp = { name: 'srivarshan', dept: 'R&D' }; // we can define object


in single line

- key : value pair are called properties


Here, name : 'Rahul' [key : value] pair

Accessing object properties


- using dot notation
objectName.Key
for eg:
Emp.name
- using bracket notation
objectName["propertyName"]
for eg:
Emp["name"]

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")
}
}

Emp.greet() // accessing object method

2.Methods
- Adding a Method to a JavaScript Object
for eg:
let Emp = { };
Emp.name = 'srivarshan';
Emp.greet = function() {
console.log('hello' + ' ' + Emp.name);
}

Emp.greet() // hello srivarshan

You might also like