var | let |
The scope of a variable defined with var is function scope or Declared outside any function, global. |
The scope of a variable defined with let is block scope. |
Examples | |
function varandlet() { var i = 5; } console.log(i); VM131:1 Uncaught ReferenceError: i is not defined at |
function varandlet() { let i = 5; } console.log(i); VM131:1 Uncaught ReferenceError: i is not defined at |
function varandlet() { For(var i=0;i<10 br="" i=""> console.log(i); } Varandlet(); // Output = 10; No error here 10> |
function varandlet() { For(let i=0;i<10 br="" i=""> console.log(i); } Varandlet(); VM350:1 Uncaught ReferenceError: i is not defined at varandlet ( at ******** LET has block level scope. |
{ var o = p = 10; } console.log( o); console.log( p); // Output // O = 10 // P = 10 |
{ let o = p = 10; } console.log( o); console.log( p); // Output // Error O is not defined // P = 10 // In this case P becomes global variable |
for (var i = 0; i < 5; ++i) { setTimeout(function () { console.log(i); // output '5' 5 times }, 100); } //Output will be five times 5 The settimeout function works in asynchronous way. |
Closure in Loop for (let i = 0; i < 5; ++i) { SetTimeout(function () { console.log(i); // output '5' 5 times }, 100); } //Output will be five times 1 2 3 4 5 let in the loop can re-binds it to each iteration of the loop, making sure to re-assign t the value from the end of the previous loop iteration, so it can be used to avoid issue with closures. |
Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts
Tuesday, July 11, 2017
Javascript : Var and Let
By
Neeraj Kumar Jha
10:50 PM
distributed system design. MongoDB, ES6, javascript, JScript, Let and var, let Vs var, linux, Modern JScript, nginx server
-->
Tuesday, December 25, 2007
how to create colon of a existing table
By
Neeraj Kumar Jha
1:24 AM
asp, asp.net, database, db, db2, dotnet, java, javascript, mysql, oracle, php, vb, vb.net
MySql
CREATE TABLE new_table_name like existing_table_name
this will create new table with all the property of old table (like primary key , auto increment)
CREATE TABLE new_table_name select * from old_table
this query will create new table with all data and and structure but property like primary key are drooped
CREATE TABLE new_table_name (ID INT auto_increment primary key ) select * from old_table
this query will create new table with old table structure and with a new column " ID " .
if ID exist in old table then it will assign the property of primary key and auto increment to ID column;
ORACLE
CREATE TABLE newTable AS SELECT * FROM oldTable WHERE 1= 2;
This will create table newTable with the same columns as oldTable.
It will have no constraints or indexes. It will have zero(o) rows in it.
CREATE TABLE newTable AS SELECT * FROM oldTable WHERE 1= 1;
OR
CREATE TABLE newTable AS SELECT * FROM oldTable;
This will create table newTable with the same columns as oldTable and with all data of old table.
SQL 2000
SELECT * INTO emp_new from emp where 1=2
This will create the table structure alone.
SELECT * INTO emp_new from emp where 1=1
This will create the table structure with data.
Tuesday, December 4, 2007
Pass Multiple Arguments to Function
In our day to day programing life we find some situation when we need to pass multiple arguments at run time . we dont know the number of arguments to be passed by the user.
JavaScript
I find javaScript more handy. JavaScript provide a special property called " arguments " which contains the array of arguments passed to the function.
we can take this in an array and by length property of array we know the number of arguments passed.
function myFunc()
{
var arguments = myFunc.arguments;
for (var i = 0; i < arguments.length; i++) {
alert("Argument number " + i + " value = " + arguments[i]);
}
}
This function can be called with any number of arguments at the runtime like
vikFunc('neeraj', 'raju');
vikFunc('neeraj', 'raju','vinay','amole');
C++
In c++ we use '...'(three dots) for passing multiple arguments.The first argument of the function represents the number of argument to be passed and the second argument '...'(three dots) represents the values to be passed.First argument is int and second argument could be any thing like int, double etc .....
we also need four things va_list which stores the arguments , va_start which initializes the list , va_arg which returns the arguments in the list and va_end which cleans up the
variable in the list. To get these we must include the “cstdarg ” header file.
va_list is like any other variable.
va_start is a macro which accepts two arguments, a va_list and the name of the variable that directly precedes the '...'(three dots ) . So, in the function a_function, to initialize a_list with va_start, you would write va_start ( a_list, x );
va_arg takes a va_list and a variable type, and returns the next argument in the list in the form of whatever variable type it is told. It then moves down the list to the next argument.
#include
#include
using namespace std;
double average ( int num, ... )
{
va_list arguments; // A place to store the list of arguments
double sum = 0;
va_start ( arguments , num ); // Initializing arguments to store all values after num
for ( int x = 0; x < num; x++ ) // Loop until all numbers are added
sum += va_arg ( arguments, double ); // Adds the next value in argument list to
sum. va_end ( arguments );
// Cleans up the list
return sum / num; // Returns some number
}
int main()
{
cout<< average ( 3, 12.2, 22.3, 4.5 );
cout<< average(5,3.3,2.3,3.5,6.7,2.9);
}
JavaScript
I find javaScript more handy. JavaScript provide a special property called " arguments " which contains the array of arguments passed to the function.
we can take this in an array and by length property of array we know the number of arguments passed.
function myFunc()
{
var arguments = myFunc.arguments;
for (var i = 0; i < arguments.length; i++) {
alert("Argument number " + i + " value = " + arguments[i]);
}
}
This function can be called with any number of arguments at the runtime like
vikFunc('neeraj', 'raju');
vikFunc('neeraj', 'raju','vinay','amole');
C++
In c++ we use '...'(three dots) for passing multiple arguments.The first argument of the function represents the number of argument to be passed and the second argument '...'(three dots) represents the values to be passed.First argument is int and second argument could be any thing like int, double etc .....
we also need four things va_list which stores the arguments , va_start which initializes the list , va_arg which returns the arguments in the list and va_end which cleans up the
variable in the list. To get these we must include the “cstdarg ” header file.
va_list is like any other variable.
va_start is a macro which accepts two arguments, a va_list and the name of the variable that directly precedes the '...'(three dots ) . So, in the function a_function, to initialize a_list with va_start, you would write va_start ( a_list, x );
va_arg takes a va_list and a variable type, and returns the next argument in the list in the form of whatever variable type it is told. It then moves down the list to the next argument.
#include
#include
using namespace std;
double average ( int num, ... )
{
va_list arguments; // A place to store the list of arguments
double sum = 0;
va_start ( arguments , num ); // Initializing arguments to store all values after num
for ( int x = 0; x < num; x++ ) // Loop until all numbers are added
sum += va_arg ( arguments, double ); // Adds the next value in argument list to
sum. va_end ( arguments );
// Cleans up the list
return sum / num; // Returns some number
}
int main()
{
cout<< average ( 3, 12.2, 22.3, 4.5 );
cout<< average(5,3.3,2.3,3.5,6.7,2.9);
}