Teaching Some Code (JavaScript: Variables, Constants, Functions, Datatypes)
This time around, I'm going to be helping with JavaScript.
If your using chrome, you can run the code discussed in this thread in your console by hitting `ctrl+shift+j`
Let's start with something simple, Variables. A variable is something that stores data and can be changed later. You can define a variable by doing something like the following: ``` var a = "text"; ``` This will define the variable "a" with the value of "text" if you were to type in the console "a" after running this snippet of code, it would return "text" as seen in the attachment
Does that makes sense so far?
Yes sir :P
Yesh. e.e
Alright, you guys try now, define a variable and call it
Can't I'm on mobile x.x
Oh, something I forgot to mention. Syntax. at the end of each code line you want to have a `;`, that declares the end of the line. It's not required in JavaScript, but it's very good to know and a good practice to follow.
``` var b = "test"; ```
Like that? o:
Indeed. Now then, constants. Similar to variables a constant stores data, however a constant cannot be changed after being defined You can define a constant by using `const` in place of `var`. Attempting to change a constant after being defined will result in a error, which you can see in the attachment.
Make sense?
Yes.
(code used in above example) ``` const example = "Hello World!"; example = "Example"; ```
You can never change a constant? o:
Nope
It does :P
Is it necessary to include `example = "Example";` ?
No, that was just to show that it would throw a error attempting to change the value
Ooooooo ``` const example = "test"; ```
Correct?
Indeed.
Now let's talk about the different datatypes that one can pass to a variable. The main ones are as follows: Integers: Numbers, such as 5 or 292 Strings: Text, such as "testing" Objects: More on this later, it is essential a key -> value data store such as `{"a":"b"}` Arrays: More on this later, Multiple values in the same string, example would be `["test",1,{"a":"b"}]`
``` ["testing",5,{"b":"c"}] ``` e.e
Arrays aren't just limited to that by the way They can be something like this: ``` ["test","example","dog"] ``` Or something much more complex combining all other data types into one.
Make sense?
Yes I get it ;P
How do you combine all the other data types in one? e.e
The first example I gave, gave a example of that. We'll get more into that later
Does this all make sense so far dude?
Yes, though `ctrl+shft+j` doesn't work for me
Because you're on mac. It's `cmd+opt+j` on mac
Now let's talk about a comment A comment is something that can be written down in the code, but won't execute. A single line comment is done with `//` A multi line comment starts with `/*` and ends with `*/`
In some languages, a single line comment is `#` (pointing that out incase anyone looks at other languages)
Make sense so far?
HeydontmentionotherlanguagesweareonlytalkingaboutJavaScript,itcangetconfusingsometimes at least for me
Noted
What about you @Elsa213
K elsa died, moving on
Next let's do functions. ``` function function_name() { //code goes here } ``` That is the basic syntax for functions. You can run what's in a function by calling it Calling something refers to running it's name For a variable you would run it's name, however for a function, you would run it's name with () for example, in this instance we would call our function with `function_name()` Make sense so far?
What kind of function :P
If the name is longer than a word does it use underscores as spaces?
Correct, or you can just capitalize the next word likeThis
Now lets talk about how you can pass data to a function, and return data from a function You can pass data to a function by defining a variable with the function within the parenthesis. You can return data by using the `return` keyword. This also stops execution of the function any further. As a example: ``` function example(hello) { return hello; } ``` This will return whatever is passed to the first variable of the function. Passing data to the function when running it is done by having that data in the parenthesis when running it ``` function example(hello) { return hello; } example("Hello World!"); ``` This example will return "Hello World!" to the console. Still with me?
Yes :P
Yup
You can pass multiple parameters to functions by separating each value with a "," for example... ``` function example(hello,world) { return hello+world; } example("Hello","World"); ``` the `+` in this example combines the 2 variables together, and then the `return` outputs them. Make sense?
Yes :D
Dude you still with me?
Yes x'D
Alright, next let's do a assignment to make sure you guys were paying attention =P I want you to construct a small script, that defines a variable, and a constant, as well as a function. That function should be declared accepting 2 parameters, outputting them both. You should then pass the variable and constant you declared before the function to the function.
Judging by the fact that it's been 20 minutes, and it took me 15 seconds to write what I described above (although I expected it would take a maximum of ~5 minutes), I assume you're all struggling on this?
So I can define a variable, constant but having troubles with functions
Let me see what you got and I can explain what went wrong ^_^
I already defined dood
You're telling the function to return itself, not the variable you defined in the function scope "testing"
oh, well then
How would I fix that?
change the return line to ``` return dood+testing ```
Ohh okay I see
The `+` doesn't put a space?
No, just combines the two variables.
It also adds if you have 2 integers I.E. ``` var a = 42; var b = 91; var c = a + b; ```
I've tried that before ;o
@Elsa213 Are you following so far? =P
Should I reveal the answer, or do you have it now dude?

Should I reveal the answer?
wait answer? o-o
the answer to the assignment I gave...
Oh well I guess I was the only one that did the hw ¯\_(ツ)_/¯
(it's pretty freeform, but in the end still gets the same result)
NO IM GERE
How do you type the code out with colors?
\```javascript code here \```
Ah like discord x'D
I mean, technically discord is like us, even OS had it like that long before discord was even a thought lol
Shall we move on?
Or are you still catching up TGP?
Catching up, one second
Okie, I copied and pasted the steps in the colors of red and purple. Purple = important. Red = very important. Got it
You said you were going to try to do the assignment?
Yes sir
waits
I don't get where to put the variable at :|
You define the variable before the function
do i remove everything? o-o
(or after, doesn't really matter so long as it's defined before it's used)
What do you mean do you remove everything? O_o What do you have so far?
that part
Don't worry about that, it has nothing to do with any code you were doing x'D
Oops LOL
wut to do for the code thingy
hrmmm |dw:1520038200661:dw| uhhh like dis?
spelled celestial wrong >.>
How ._.
Yes, however you forgot one part, you were supposed to pass a variable to the function (by defining the variable before using it)
hrmmm
Bg how'd you do that
im confused e.e
Alright, let me give you example
``` var hello_world = "Hello World!"; function return_var(value) { return value; } return_var(hello_world); ``` Output is "Hello World!" Get it now?
*hits head on desk repeatly*
i think so e.e
Alright, try it =P
TGP where are you stuck?
Where to type it in 😂
The chrome console
You had it there
OHHH
so like dis? |dw:1520039955836:dw| hrmmmm
honestly im still confused >.>
Back, had to eat dinner. You did it correctly, now split it into 2 variables (1 of them a constant)
@563blackghost
hrmmmm e.e
Should I show you the solution?
let meh try one more time, then put it okie?
Kk
what do you mean by split to two variables? i add another besides star? (in my case dat is)
How come when you define a variable, you get returned with "undefined"
Because you didn't specify anything to be returned, so since nothing returns, the return is undefined shadow
I mean have 2 variables like you did in your first attempt, but using variables instead (and one of those variables using the `const` prefix)
(as in predefined BOTH values you pass to the function)
Can you define multiple variables in the same line?
Yes, like this ```javascript var example="a",test=2,extra="testing"; ``` Put a comma inbetween each variable you want to define. You can also define variables without a value until your ready for a value like this: ```javascript var example,test,extra; var example = "a"; var test = 2; var extra = "testing"; ```
i dont get it ultri ;-; i understand adding the var but i dont know how to add da consts ;-; its const example = "test"; right?
Correct
BTW you might want to put code like this: \`code here\`
```javascript const people = "something" var ghost = "boo" ```
```javascript const vara = "Hello"; var varb = "World!"; function demo(a,b) { return a+b; } demo(vara,varb); ``` Alright, here's how it should look =P
O-O
oh
I read coordinate points.
*taking note*
[\begin{ var a = "a";} \]
How do you make a block of code * '^' *
Okay look @mhchen
do
No do this ```
``` words under it
``` test ``` see?
Normal Text ``` var code = "Codey Text"; ``` Normal text
Yesh I got it. Can't wait for Ultri's next javascript tutorials.
``` test ``` ``` test ``` ``` test ``` ``` test ``` ``` test ``` ``` test ``` And the boxes can be repeated and repeated as shown ^
could you teach me the code of the jedi and the code to be mod?
Join our real-time social learning platform and learn together with your friends!