Professional Documents
Culture Documents
● Javascript code only can run in the browser so its a Javascript Engine
● Browser and node provide a runtime environment for a javascript code
● Console: How does Javascript run in the browser?
“Let” does not allow to redeclare variables and “var” allows to redeclare variables
Variables:
// // Variables in Javascript
let name = 'collegeranker';
console.log(name);
//Rules:
//Can not be a user keyword
//meaningfulcannot start with a number
//Cannot contain a space or hyphen (-)
1
Constants: they always remain the same throughout the program and can not be
changed when we use “const”.
// Constants = const
let i = 0;
while(i<=9){
console.log(i);
i++
}
let i = 0;
do{
console.log(i);
i++;
}
while(i<=10)
2
For Loop: These are very important in javascript. As we iterate over a loop to print
things to some sort of conditions.
//for loop
3
The for in Loop:
4
Datatypes in javascript:
● Primitive/Value Datatypes:
1. String
2. Number
3. Boolean
4. Undefined
5. null
● Reference Datatypes:
1. Objects
2. Arrays
3. Functions
5
Objects:
An object is an instance that contains a set of key-value pairs. Unlike primitive data types,
objects can represent multiple or complex values and can change over their lifetime.
example: car, pen, bike, chair, glass, keyboard, monitor, etc
let person = {
personname: "shivam",
age: 22,
};
// Dot notation
person.name = "onkar";
//Bracket Notation
let selection = 'name';
person[selection] = "me"; //let['name'] = 'me';
console.log(person.name);
Arrays:
An array is an object that can store multiple values at once. For example, const words = ['hello',
'world', 'welcome']; Here, words is an array.
Note:
● In Javascript we can store different types of elements means we can use elements of
different data types.
●
● In other languages that are not like same as Js. in those languages only we can store is
the set of similar datatypes.
// //Arrays
6
console.log(selectedColors.length);
//or
console.log(selectedColors[0]);
Functions: A function in JavaScript is similar to a procedure and set of statements that perform
a task or calculate a value.
// //Functions
// // Performing a task
function greet(name, lastName) {
console.log('hello' + ' ' + name + ' ' + lastName);
}
greet("jhon", "smith");
// //Types of functions
// Calculating a value
function square(number) {
return number*number;
}
console.log(square(2));
7
Javascript VIMPS:
Best way to understand when to use var, let, and const with three concepts.
● Scope
● Reassigning a new value
● When you access a variable before declaring