var itemClassName = "carousel__photo";
items = d.getElementsByClassName(itemClassName),
totalItems = items.length,
slide = 0,
moving = true;
I saw this part of the code on the medium.com website while creating a review carousel. how did they declare this variable? I can not understand this declaration and they use d instead of document how? and why? I am a beginner... if you need more information kindly visit medium.com site .bcuz of your clarity I added only the part I got confused
That article has used IIFE JavaScript functions. you could read more about that concept from the link that original article said or from any other tutorial like this one. simply it is a way of running a function without need to call it in your code. for better understanding I converted the script of original article to a normal function.
this is the article code:
(function(d)
{
var itemClassName = "carousel__photo";
items = d.getElementsByClassName(itemClassName),
totalItems = items.length,
slide = 0,
moving = true;
console.log(itemClassName);
})
(document);
And this is the code I converted:
let documentVar = document;
function carousel(documentArgu) {
var itemClassName = "carousel__photo2";
items = documentArgu.getElementsByClassName(itemClassName),
totalItems = items.length,
slide = 0,
moving = true;
console.log(itemClassName);
}
carousel(documentVar)
In the converted version you need to call carousel(documentVar) function to run it. But in the article code you do not need to call function. using IIFE has other advantages also that you can read more from tutorials. But about your question that why d stands for document, the reason is that you could pass IIFE function arguments in the last parenthesis and that means the argument d used in function(d) is the same document that was defined in (document) in the last of article code.