Yousef Essam
Yousef Essam

Reputation: 161

What are the values that can be assigned to variables in javascript?

In all programming languages "variables" can be defined as follows :

"They are reserved places in RAM to store data"

Turns out that such code is logical in javascript:

var x = document.getElementById("IdName");
x.innerHTML = "Hello Stack Overflow";

Or This Code:

var x = alert("Hello Stack Overflow");

I don't get it, Of course alert() and document.getElementById("")

aren't data to be assigned to variables

I want someone to explain why such thing is possible.

I'm really confused of this.

Upvotes: 0

Views: 52

Answers (1)

deceze
deceze

Reputation: 522135

No, document.getElementById("IdName") "isn't" data; it's a function call that returns data:

Returns a reference to the element by its ID [...]

Syntax

element = document.getElementById(id);

Parameters

id
is a case-sensitive string representing the unique ID of the element being sought.

Return Value

element
is a reference to an Element object, or null if an element with the specified ID is not in the document.

https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById

The function call returns an object of type Element (or null), which is data a value that can be assigned to a variable. This works pretty much exactly the same in virtually all programming languages. Values can be assigned to variables. Functions return values.

alert() does not happen to return anything, which means it implicitly returns undefined, so the value undefined will be assigned to x. That is a rather useless operation, but still works by the same rules.

Upvotes: 1

Related Questions