Understanding Closures in JavaScript.

Downloaded photo from Unsplash
function addLastName(fname){
lname = ' Wick'
function getFullName(){
return fname + lname;
}
return getFullName();
}
console.log(addLastName('John')) // logs - John Wick
function addLastName(fname){
lname = ' Wick'
function getFullName(){
return fname + lname;
}
return getFullName;
}
let getName= addLastName('John');
console.log(getName()) // logs: John Wick
function addLastName(fname){
lname = ' Wick'
return {
setLastName: function(name){lname = name},
getName: function(){return fname + lname}
}
}
let obj = addLastName('John')
console.log(obj.getName()) // logs: John Wick
obj.setLastName('BabaYaga')
console.log(obj.getName()) // logs: John BabaYaga

Amor fati, Find my small house for thoughts @https://codeshinobis.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What to expect when migrating to Ionic 4

The ultimate guide to modern web application performance

Running osquery as sudo/root vs. user

What if React Native could be used to build high-fidelity apps AND add more than 11 platforms?

Hello @everyone👋

Event Listeners and Parent Nodes

Backend Developpement with NextJs Introduction Tutorial

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aditya Dhanraj Tiwari

Aditya Dhanraj Tiwari

Amor fati, Find my small house for thoughts @https://codeshinobis.com

More from Medium

Javascript, JSON

My understanding of JavaScript filter

Advance Objects: JavaScript

JavaScript Fundamentals