Whenever you access the internet, most of the sites you access are powered by JavaScript. This article will show you how to write your first JavaScript code and introduce you to JavaScript datatypes.
Getting started with JavaScript
JavaScript is the most popular programming language in the world. It is the programming language that powers the internet. In this article, you will learn the basics of JavaScript language.
Check this out to learn how to link a JavaScript file to a HTML file.
Logging 'Hello World" to the console.
To link a JavaScript file to a HTML file, one needs to add the following to the HTML file. <script src="myscripts.js"></script>
. The .js
file needs to be in the same folder as the HTML file for the code to work. To ascertain that the JavaScript file is properly linked, you will print hello world as shown;
console.log('Hello World');
// The console displays 'Hello World"
JavaScript Datatypes
JavaScript has 8 Datatypes.
- String
- Number
- Bigint
- Boolean
- Undefined
- Null
- Object
1. Strings
Strings are a series of characters written with either single or double quotes. They include special characters, numbers, letters, or a combination of any of them. Strings are immutable and any strings created hold value that cannot be changed. Any operations that appear to change create new strings with the desired modifications. Here are examples of strings;
"John"
"Car"
Let owner1 = "John"
let machine1 = "car"
In all instances the words "car" and "John" are used as strings.
Number
In JavaScript, Number data type is used to represent floating-point numbers and integers. JavaScript uses 64-bit floating-point representation for numbers enabling it to represent small and large numbers with high accuracy. However, it limits its ability to represent some numbers accurately leading to approximate values when used in a calculation.
let a=10;
let b=23;
let c=a+b;
console.log (Z)
The variables involved in the above example are numbers and can be used to make arithmetic calculations.
Bigint
Bigint data type in JavaScript is an numeric primitive that is used to represent integers with arbitrary magnitude. It allows storing integers that are larger than the maximum safe integer limit for Numbers data types. To create a BigInt, you append n to the end of an integer or call BigInt():
Let x = 555555555555555
Let y = 555555555555555n
let z = BigInt(555555555555555)
X is a number, while both y and x are BigInt data types.
Boolean
When programming, you will make a lot of decisions that are either true or false. To achieve this, you will need to use Boolean values where you choose from one of two values. They include YES / NO, ON/OFF, and TRUE/FALSE.
let isTrue = true;
let isFalse = false;
Booleans can be used in comparisons and conditions where the value of Boolean expressions is their basis.
let age = 20;
If (age>=18) {console.log (`You are an adult`) else {Console.log (`You are not yet an adult`)
// Output: You are an adult
If the condition is true, the console displays that the person is an adult, if it's false, it shows that the person is yet to be an adult.
When using Boolean as functions, everything with a value is true while anything without is false. False values include empty strings, undefined, null, false, and NaN.
let x = 10 / "year";
Boolean(x);
// Output : NaN
Year is not a number hence its output will be NaN
which is false in functions.
Booleans can be used as objects but it is not recommended since they can produce unexpected results. Additionally, the new keyword used to define them complicates code and reduces its efficiency.
Undefined
It is a primitive data type that is a variable in the global scope. It represents a variable that has not been assigned a value. A statement can also return undefined if the variable being evaluated lacks an assigned value. A function returns undefined if a value was not returned. Although you can use undefined as a variable name it is not recommended since it will maintaining and debugging code difficult.
let x;
typeof x === "undefined"
Null
The null
value is used to represent intentional absence of any object value. It is treated a falsy for Boolean operations. null
expresses a lack of identification indicating that the variable used lacks identification showing that it does not point to any object.
boy;
The value boy
is not identified hence its type is displayed as null.
Variables and Values
Variables are containers used to store data values. In JavaScript, variables are declared using Var, Let, and Const.
The Const variable is used to declare general rules that are fixed. For example:
const carPrice = 100000;
const extras = 20000;
Let total = carprice + extras;
console.log (total)
The code above shows that the price of the car and extras is constant. On the other hand, the let variable used to reference the total is not constant.
The Var variable is also used to declare variables that are modifiable.
var carType = Audi;
var carModel = Sports;
console.log (carType +" "+ carModel)
Variables are often declared without a value. A value ranges from something that will be provided later or one that requires calculating. Variables declared without a value are given the value Undefined. In the example given above, the value of the variables used above will change to undefined after the command is executed.
Thanks for reading, I hope you have learnt something.
Top comments (0)