For that let's just play around with javascript and I'm in a brand new empty folder which I opened with
my IDE, visual studio code and in there, I'll create a new file and I'll name it play.js, the name is totally
up to you though.
Now let me start with some core language features, variables and functions
and again you should definitely dive into a core javascript resource like the ones I linked in the second
lecture of this module
if you are brand new to Javascript,
so I by now do expect you to know what variables functions are, I also expect you to be aware of what if statements
or loops are,
these are all core things you got to have set by now because they are crucial for javascript no matter
if you are using it in the browser or on the server.
So for variables, you might know this syntax with the var keyword,
you can have var name and for example store Max in there, right.
This would be a variable and now you can use that for example to simply output it like this
and if we now run this file with nodejs by running node play.js, this is the command we have to
write in the terminal, navigate it into that project folder, so into that folder where I'm having this play.js
file, then I see Max down there, right
so that is some Javascript code.
We can also create another variable, age which now would be a number,
it's not enclosed in quotation marks, so this first one is a so-called string,
this is a number,
we can also add a third one, hasHobbies and that would now be a boolean,
these are some core types
Javascript knows.
True false are booleans, numbers are well numbers,
they can also have decimal places,
these are strings.
And now here I can also define a function with the function keyword summariseUser,
this is how you create a function in javascript and there, we could return something,
functions can return values with the return statement,
they all can get input like user name, user age and user has hobby, you can name these arguments as these
things here are called,
however you want.
These are now local variables only available inside of the function, not outside of it
and then you can return
name is plus user name plus let's say some text with a comma, a whitespace, age is whitespace, user age
plus and user has hobbies and then user has hobby, something like that.
Now here I am simply returning some text which I manually concatenating by a hardcoded strings and the
dynamic values I'm getting as an argument.
Now we can also console log the result of summariseUser and we call a function by adding parentheses after the
name
and now we just have to pass in data we expect here as arguments, so we need to pass in three arguments
here, the name, the age and has hobbies, so the three variables
I defined up here. We could have directly accessed them in the function too by the way,
we don't have to take the way of accepting arguments but this is a pure function which does not depend
on anything from outside the function but gets all the data it works with as part of the arguments which
is a nice way of writing functions.
And now if I execute this, well then we get this output here.
Now this is just a summary of the features you should be aware of
as I mentioned, I just want you to know or I just want to be sure that you know how we work with variables,
how we would work with functions, how we call functions
and that you understand how parameters or arguments in the function are used in there and only in there,
we couldn't use user name outside of there because of something called scoping in javascript,
we can then call a function like this and pass in the data,
we could hardcode the data here or refer to variables which have to be available in the scope of this statement
which is the case for these variables since they are global variables and these are variables and functions
and how we interact
and these are some core of features you have to be aware of.
Let's now dive into something more exciting in the next lecture.