Javascript – Introduction and variables

Javascript born in 1995 and initially inspired from Java. Is the only language you can run in a web browser and gives interactivity to the HTML documents. Javascript can also run outside the web browser on a nodeJS interpreter for example.

Javascript is an interpreted language and compiled just-in-time which means that the code is converted just before runtime. With Javascript, you can change the CSS and HTML code, use geolocation and even work with data using Ajax.

You can put your Javascript code:

  • inside the HTML
, <span>Javascript – Introduction and variables</span>, KandZ
  • in a local file with .js extension. You can load a Javascript file:
, <span>Javascript – Introduction and variables</span>, KandZ

variables

Variable is a container for a value, which can be an object, text, number etc. Javascript is a weakly typed language. That means there is no need to declare the type of variable, and you cannot do that. To declare a variable you need to use var or let keywords and then the name of the variable

, <span>Javascript – Introduction and variables</span>, KandZ

Then you have to initialize the variable if you want to use it:

, <span>Javascript – Introduction and variables</span>, KandZ

You can combine the above two steps:

, <span>Javascript – Introduction and variables</span>, KandZ

You can declare multiple variables:

, <span>Javascript – Introduction and variables</span>, KandZ

or declare a constant variable. A constant variable is a variable that its value cannot be changed:

, <span>Javascript – Introduction and variables</span>, KandZ

Use console.log(variable) to print a variable value at the console:

, <span>Javascript – Introduction and variables</span>, KandZ
, <span>Javascript – Introduction and variables</span>, KandZ

In Chrome use Ctrl + Shift + I to open the developer tools and console.

Variable scope

Javascript has two scopes:

  • Global scope
  • Block scope

Global scope: You need to declare the variables outside any functions and code block {}. You have to use the var keyword. Those variable are available in the whole code. Block scope variable with the same name can mask the global variable inside a block.

Block scope: You have to use the let keyword to declare the variables and must be inside a code block {}

THE END!!!

100690cookie-checkJavascript – Introduction and variablesJavascript – Introduction and variables

Related posts