JavaScript Basics – Data types, Variables, Constants and Conversion between data types
JavaScript is a programming language that adds interactivity to your website. This happens in games, in the behavior of responses when buttons are pressed or with data entry on forms; with dynamic styling; with animation, etc. This article helps you get started with JavaScript and furthers your understanding of what is possible.
Hello, world!
This part of the tutorial is about core JavaScript, the language itself.
But we need a working environment to run our scripts and, since this book is online, the browser is a good choice. We’ll keep the amount of browser-specific commands (like alert
) to a minimum so that you don’t spend time on them if you plan to concentrate on another environment (like Node.js).
So first, let’s see how we attach a script to a webpage. For server-side environments (like Node.js), you can execute the script with a command like "node my.js"
.
The “script” tag
JavaScript programs can be inserted almost anywhere into an HTML document using the <script>
tag.
For instance:
- Go to your test site and create a new folder named
scripts
. Within the scripts folder, create a new file calledmain.js
, and save it. - In your
index.html
file, enter this code on a new line, just before the closing</body>
tag:<script src="scripts/main.js"></script>
- This is doing the same job as the
<link>
element for CSS. It applies the JavaScript to the page, so it can have an effect on the HTML (along with the CSS, and anything else on the page). - Add this code to the
main.js
file:const myHeading = document.querySelector('h1'); myHeading.textContent = 'Hello world!';
- Make sure the HTML and JavaScript files are saved. Then load
index.html
in your browser. You should see something like this:
JavaScript Data Types
JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript.
- Primitive data type
- Non-primitive (reference) data type
JavaScript is a dynamic type language, means you don't need to specify type of the variable because it is dynamically used by JavaScript engine. You need to use var here to specify the data type. It can hold any type of values such as numbers, strings etc. For example:
- var a=40;//holding number
- var b="Rahul";//holding string
JavaScript primitive data types
There are five types of primitive data types in JavaScript. They are as follows:
Data Type | Description |
---|---|
String | represents sequence of characters e.g. "hello" |
Number | represents numeric values e.g. 100 |
Boolean | represents boolean value either false or true |
Undefined | represents undefined value |
Null | represents null i.e. no value at all |
JavaScript non-primitive data types
The non-primitive data types are as follows:
Data Type | Description |
---|---|
Object | represents instance through which we can access members |
Array | represents group of similar values |
RegExp | represents regular expression |
- Log in to post comments