+ 10

JAVASCRIPT LOGIC????

1.)Why does it print undefined in place of uninitialized? var a; console.log (a); 2.)It prints a is not defined...then what is the difference between undefined and not defined? console.log (a);

21st Jun 2019, 12:46 AM
John Dhinakar
John Dhinakar - avatar
12 odpowiedzi
+ 18
[Part 2 of 3] Now... consider the following examples of declared and undefined variables: var k; let j; const x = undefined; y = undefined; NOTE 1: The lines below are equivalents: var k; var k = undefined; NOTE 2: Variables declared using `const` must immediately be assigned a value. In the above example, {x} is a constant that is assigned the value {undefined}. NOTE 3: Undeclared variables can be assigned the {undefined} value as seen with variable {y} in the example above. NOTE 4: {undefined} vs "not defined" {undefined} is the name of a global object. "not defined" is text in an error message that appears when accessing a variable that is not declared and not assigned a value. NOTE 5: {e} is declared, {f} is not declared. let e = f; Hopefully, this clears up a lot of the confusion. 😉👌
21st Jun 2019, 3:40 AM
David Carroll
David Carroll - avatar
+ 13
[Part 1 of 3] Expanding on Sonic's answer, undefined is a read-only, primitive, global object in Javascript that is automatically assigned to variables that are declared without a value assignment. You can learn more about this in the following link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined Variables are declared when followed by the keywords var, let, const, or listed as input parameters in a function or lamba expression. In weakly typed languages, like Javascript, the type of a declared variable can only be inferred from an assigned value. Examples: let a = 4; // number let b = "b" // string let c = true; // boolean However, when a variable has not been assigned a value, the type cannot be inferred: //Defaults to global object: undefined let d; Finally, when attempting to reference a variable, let's call it {k}, that has NOT been declared and has NOT been assigned a value, a ReferenceError will be thrown with a message like: "k is not defined"
21st Jun 2019, 3:13 AM
David Carroll
David Carroll - avatar
+ 12
[Part 3 of 3] Dhinakar Reddy Challa Apologies... I overlooked your clarification for initialization. Initialization occurs when the initial value is assigned to a variable. Based on my understanding of this in Javascript, this can only occur when a variable is declared or when an undeclared variable is assigned a value. Examples: //Initialized with {undefined}. let x; //Initialized with a number 4. let y = 42; //Initialized with a string value "foo". z = "foo"; So... in this case, I believe initialization will either be a default value pointing to the global object {undefined} or to a value explicitly assigned to the variable when declared. Here are a few links you can review for more information on variable initialization: https://teamtreehouse.com/community/variable-initialization-in-javascript https://stackoverflow.com/questions/31714434/declaring-vs-initializing-a-variable http://www.ecma-international.org/ecma-262/6.0/#sec-variable-statement
21st Jun 2019, 6:29 AM
David Carroll
David Carroll - avatar
+ 10
Variable declaration is not the same as definition. For a to be defined, you need to assign it a value.
21st Jun 2019, 2:16 AM
Sonic
Sonic - avatar
+ 9
David Carroll that was a prime example of a helpful, comprehensive and authoritative answer!
21st Jun 2019, 4:58 AM
Sonic
Sonic - avatar
+ 6
Undefined means not defined..till that point its all well and good....even if I don't create a variable and console it ...it prints not defined. ..I mean whats the sole difference
21st Jun 2019, 12:53 AM
John Dhinakar
John Dhinakar - avatar
+ 6
Interesting article that explains the difference between undefined and null (and easy to understand 😉) http://www.tothenew.com/blog/difference-between-undefined-and-null-in-javascript/ And after reading that maybe you think what about NaN? Explained here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN
21st Jun 2019, 8:34 AM
Robin R.
Robin R. - avatar
+ 5
Can u describe each of them...for a clarity. ...definition,declaration, initialization
21st Jun 2019, 2:26 AM
John Dhinakar
John Dhinakar - avatar
+ 5
David Carroll You explanation is beyond words. In your explanation I didn't come across the word initialization...which means there is no such word in javascript if am not wrong?
21st Jun 2019, 5:49 AM
John Dhinakar
John Dhinakar - avatar
+ 5
I just want to add this simple code as an example in practice https://code.sololearn.com/WqcGainBs6iu/?ref=app
21st Jun 2019, 7:30 AM
Robin R.
Robin R. - avatar
+ 4
Dhinakar Reddy Challa i think you mean why it is printing undefined since i declared the variable, it is undefined because its value is undefined until you initialize to a value, both when you don't declare a variable and when you declare it but not initialize it prints undefined because the issue is the value of the variable not the name of the variable hope this will help a little bit
21st Jun 2019, 6:58 AM
Hafsa Mohamed
Hafsa Mohamed - avatar
0
var a; console.log (a) Не получается потаму что ты не присвоил переменной значение
27th Apr 2020, 8:00 AM
NAT 24
NAT 24 - avatar