Q.s 1 :- Write a difference between var vs const vs let.
Q.s 2 :- Explain template literals with example.
Q.s 3 :- Write a Javascript program to triple all numbers in a given Array of integers. Use map.
Given :- nums = [1, 2, 3, 4, 5, 6, 7]
Q.s 4 :- Write a Javascript program to filter employee name from the given structure
Given :-
let emp_info = [ { emp_name : "john", emp_email : "john@gmail.com", contact : 98765789 }, { emp_name : "tom", emp_email : "tom@gmail.com", contact : 98765789 } ]
**1. **
# var is the older way of declaring variables in JavaScript.
# let is also used to declare variables, and it has block scope as well.
# const is used to declare constants in JavaScriptstrong text
Q1.
var :- A keyword used for declaration global scope variable.
We can change the value of the variable as well as initialize different values
with the same variable name.
let :- A keyword used for declaration block scope variable.
We can change the value of the variable but cannot initialize different values
with the same variable name within same block.
const:- A keyword used for declaration block scope variable.
In this keyword neither we can change the value of the variable nor initialize
different values with the same variable name within whole JS code .
Que1 :- 1) we can reuse the var in through out the code 2) let keyword we can not use it again cox its block scope. 3) const keyword we can not reassign it.
var const and let is a variable used in javascript as a container in a form of predefined…to store values
var is a variable that is used multiple times redeclared…
let is a variable that cannot be redeclared it is block scope
const is also a variable…same as blockscope…cannot redeclared…reassigned
ans1.
var - is a global scope and can be declare multiple times.
let - is a block scope and be declare once inside the block.
const - const is constant variable and can not be declare more than once and its a block scope.
ans2.
let a = [1,2,3,4,5]
a.filter((element){
document.write(`${element > 2}`)
})
var is a function scope and let is a blocked scope.
var variable can be updated and re-declared within its scope.
let variable can be updated but not re-declared.
const variable can neither be updated nor re-declared. they are all hoisted on the top of their scope.
TEMPLATE literals…is ${}
let data1 =“hello”;
let data2 =“how r u?”
document.write(${data1} john ${data2}
);
Answer1:
Var: var is for variable declartion, you can redecalare it and it is global scope. Means you can call var variable any where in the javascript. And always last update value will print in the end.
Example
var k=0;
var k=1;
var k=2;
console.log(2)
output: 2
let: let is for variable declartion, you can not redecalare it and it is block scope. Means you can call var variable in only {} blocks. And always last update value will print in the end.
const: let: let is for variable declartion, you can not redecalare it’s value and name and it is block scope. Means you can call var variable in only {} blocks. And always last update value will print in the end.
example: wrong way
right way
ans3.
nums = [1, 2, 3, 4, 5, 6, 7]
var ans = nums.map((element)=>{
return element*3
})
document.write(ans)
const name = “mani”;
const age = 21;
const city = “mumbai”;
const answer = Hello, my name is ${name}. I am ${age} years old and I live in ${city}.
;
console.log(answer);
Q2.
Template literals are nothing but a modern JavaScript(released in ES6 version)
way of printing the results.
Eg:-
let data1= "Hello Everyone!";
let data2= "My name is";
let data3= "Sayantan";
document.write(`<b style="color:blue;"><i> ${data1} ${data2} ${data3} <i></b>`);
Ans -01
var is a Global scope variable and a good advantage of var is we can are able to re-use the var name many time and the disadvantage is it take good confusion because of it can have one name in many line
let is a block scope variable and it can’t be decreeable in one block entry for re-use the same the same let we need to open new block
const is a block scope variable we use const to create sensitive data and it can’t be changeable it’s have fixed data
Template literals provide an easy way to interpolate variables and expressions into strings .using back-ticks (``) rather than the quotes (“”) to define a string.
Ex : let data4 =“hello”;
let data5 = "how are you ? "
let data6 = “tom”;
document.write(${data4} <br> ${data6} demo msg 2 ${data5}
)
the key differences between
var,
const, and
let` in with example
-
Scope:
var: Function-scoped, visible within the function they are declared in or globally if declared outside in any function
const and let: Block-scoped, visible only within the block they are declared inExample:
if (true) { var x = 10; // Function-scoped const y = 20; // Block-scoped let z = 30; // Block-scoped } console.log(x); // Output: 10 console.log(y); // Error: y is not defined console.log(z); // Error: z is not defined
-
Hoisting:
vardeclarations are hoisted to the top of their scope but are initialized with `undefined
const and le declarations are also hoisted but are in the “temporal dead zone”(TDz until the actual declaration is reachedExample:
console.log(a); // Output: undefined var a = 10; console.log(b); // Error: Cannot access 'b' before initialization let b = 20;
-
Reassignment(initialization
var and let variables can be reassigned with new values.
constvariables cannot be reassigned after they are declared, but their properties or elements (for objects and arrays)can be modifiedExample:
var age = 25; age = 30; // Valid with var and let const name = "John"; name = "Mike"; // Error: Assignment to constant variable const person = { firstName: "John" }; person.firstName = "Mike"; // Valid, modifying the property of a const object
-
Global Object Property
var declarations at the global scope create properties on the global object (window
in browsers
let and const declarations at the global scope do not create properties on the global objectExample:
var globalVar = 100; console.log(window.globalVar); // Output: 100 let globalLet = 200; console.log(window.globalLet); // Output: undefined
In summary, We use const
for immutable values let
for mutable values, and avoid using var
in modern JavaScript to prevent scoping and hoisting issu
var num = [1,2,3,4,5,6,7]
var ans1 = num.map((elements)=>{
// console.log(elements)
return elements**3
})
console.log(ans1)
Ans2: During using templete litral use ``(backticks), in the we can concatinate easily with ${}, no need for using + and .concatinate method.
<script>
let k='krishna';
let y='yadav';
let z=`My first name is ${k} and last name is ${y}`
console.log(z);
</script>
- let emp_info = [
{ emp_name: “john”, emp_email: “john@gmail.com”, contact: 98765789 },
{ emp_name: “tom”, emp_email: “tom@gmail.com”, contact: 98765789 }
];
const employeeNames = emp_info.map((emp) => emp.emp_name);
console.log(employeeNames);
Que:-3
let fun1 = (element)=>{
document.write(${element} <br>
);
}
let arr2 = [1,2,3,4,5,6];
arr2.map(fun1);
document.write(*******<br>
)
arr2.map((element)=>{
document.write(${element*3} <br>
)
});
Q3.
let nums = [1, 2, 3, 4, 5, 6, 7];
let ans= nums.map((element)=>{
return element**3;
});
console.log(nums);
console.log(ans);