After an Intro that was more about theory and preparation, in this part we will jump right into programming and write our first commands. I suppose that you already have some software for writing you code, so let’s look at our first subject – basic methods.
First we have to learn is a couple of methods first. These methods will help you for example to “print” the content of variables you defined. Don’t worry, you don’t need any printer or other stuff. Here is what I mean by print …
This is the first method we will talk about. Console.log allows us to print data on the screen. It will display anything you put inside the parentheses. Everything you print will be displayed in console (Developer tools). For example to display number write:
console.log(5); // print 5
console.log('dog'); // print dog console.log("house"); // print house
If you use different ending quotes than beginning:
console.log('house"); // print error - Unexpected token
console.log(dog); // print error - dog is not defined
Do you like that pop-up windows? I guess not. Do you want to know how to create them? Hmm… To display display this pop-up, just use “alert()” method. Anything that you write between parentheses will be displayed in the pop-up window.
alert(13); // pop-up window containing number 13 alert("Hello world!"); // pop-up window containing string (sentence) Hello world!
Another method is confirm(). It contains text and two buttons – OK and cancel. Also, if you will look at the console, you can see that it actualy return something. If you chose OK, returned value is true, otherwise it is false.
confirm("Do you feel awesome?"); // pop-up window with defined question confirm("Do you like comics?");
The last method you can use prompt(). This method is great for asking user for some input. You can use it to ask the user for his name, age or whatever you want, simple write the question inside the parentheses (don’t forget the quotes). Like confirm, this method also return data. Anything that user write inside the input field will be displayed in console.
prompt("What's your name?"); prompt("How old are you?"); prompt("Do you like Star Wars or Star Trek?");
If you liked this article, please subscribe so you don't miss any future post.
If you'd like to support me and this blog, you can become a patron, or you can buy me a coffee 🙂