Not sure what the confusion is.
If you have a page item (say a text frame, or rectangle or whatever), it has a property called itemLayer, which will give you a reference to the layer that the item is on.
And setting a layer to true or false will turn it on or off in the layers panel, e.g.:
Obviously you don’t have to get a reference to a layer through a page item, you can access all the layers in the document through the