JavaScript Data Types and Variables.

JavaScript Data Types and Variables.

Learn, types of data types and variables in JavaScript

JavaScript is one the various, top most used programming language in the world right now. But each languages varies from their syntax and data structure. In this article, it elaborates on primitive data type, variables and string concatenation respectively. In JavaScript there are to set of data types, which are primitive data types and non-primitive data types(objects), but in this article, we will focus on primitive data type. What are primitive data type? A primitive data type is either a data type that is built into a programming language, or one that could be characterized as a basic structure for building more sophisticated data types.

Data Types

  • Boolean Data Type: It represents a logical entity and can have two values: true and false. E.g;
let running = true;
let flying = false;
  • Null Data Type: This data type has exactly one value: null. E.g;
var a = null;
alert(a); // Output: null
  • Undefined Data Type: The undefined data type can only have one value-the special value undefined. If a variable has been declared, but has not been assigned a value, has the value undefined. e.g;
var a;
alert(a) // Output: undefined
  • Number Data Type: This type is used to represent positive or negative numbers with or without decimal place, or numbers written using exponential notation. e.g;
var a = 25;         // integer
var b = 80.5;       // floating-point number
var c = 4.25e+6;    // exponential notation, same as 4.25e6 or 4250000
  • String Data Type: This is used to represent textual data (i.e. sequences of characters). Strings are created using single or double quotes surrounding one or more characters. e.g;
let a = 'Hello!';  // using single quotes
let b = "Hello!";  // using double quotes

What is a variable?

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.

let myName;

Assigning Value to Variables

Naming variables is known as one of the most difficult tasks in computer programming. When you are naming variables, think hard about the names. When you assign a variable, you use the = symbol. The name of the variable goes on the left and the value you want to store in the variable goes on the right.

let myName = "David";

Not that values in a variable can change or reassigned another value.

let grade = 50;
grade = 70;

Concatenation

String concatenation is the operation of joining character strings end-to-end. There are three ways of concatenating.

  1. The + Operator: The same + operator you use for adding two numbers can be used to concatenate two strings. e.g;
let tool = "hammers";
let num = 5;
let message = "There are" + num + " " + tool + " in shed. ";
  1. String#concat(): The concat() function takes one or more parameters, and returns the modified string. Strings in JavaScript are immutable, so concat() doesn't modify the string in place.
const str1 = 'Hello';
const str2 = str1.concat(' ', 'World');

// 'Hello'. Strings are immutable, so `concat()` does not modify `str1`
str1;
// 'Hello World'
str2;
  1. Array#join(): The Array#join() function creates a new string from concatenating all elements in an array. For example:
['Hello', ' ', 'World'].join(''); // 'Hello World'

The first parameter to join() is called the separator. By default, the separator is a single comma ','.

['a', 'b', 'c'].join(); // 'a,b,c'

Conclusion

we learnt what primitive data type is, types of primitive data types, what are variables and how values are assigned to them, and also string concatenation and the three(3) ways of concatenating strings.

Hope this was fun, Enjoy!