DEV Community

loading...

Basic JavaScript (Part-1)

Mozibur Rahman
Programmer | Coder | Web Developer
Updated on ・3 min read

Data Types

There are two basic data types in Js. They mainly represent two different ways that data are stored in memory.

Primitive Data Types: data stored as a single value and they are immutable.

  • string ('Hi!', "Hello")
  • number (12,14.23)
  • BigInt (22n)
  • boolean (true, false)
  • symbol
  • null
  • undefined.

Non-Primitive Data Type: data stored as value correspond to memory reference and the values are mutable.

  • Object

Checking Data Type

To check the data type of a variable or literal, we can use the typeof operator.
Syntax: typeof operand
It will return the type.

console.log(typeof 23); //"number"
console.log(typeof 23); //"number"
console.log(typeof NaN); //"number"

console.log(typeof "Hi!"); //"string"
console.log(typeof true); //"boolean"
console.log(typeof false); //"boolean"

console.log(typeof {a: 1}); //"object"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object"

Enter fullscreen mode Exit fullscreen mode

Dynamically Typed Language

In a programming language, if variables value are not bound to single data types, then it is called dynamically typed language. Javascript is a dynamically typed language. A variable can number and then it can be reassigned to string or other data types.

// no error
let greetings = "hi";
greetings = 789;
Enter fullscreen mode Exit fullscreen mode

Number

There is a total of five types of numerical values in Number Type.

  1. integer
  2. float
let value = 456; // integer
value = 4.56; // floating point number
Enter fullscreen mode Exit fullscreen mode

Others three are special Numeric values.

  1. Infinity: It is greater than any number.
  2. -Infinity: It is lower than any number.
  3. NaN: It means there is a computational error or incorrect or undefined operational result.
let value = ( 1 / 0 ); // Infinity
value = "asdf" / 2 ; // NaN
Enter fullscreen mode Exit fullscreen mode

Number Parsing

We can parse a string to number using the parseInt and parseFloat function. they are top-level functions and not associated with any local object. They are the function property of the global object. The parseInt() function parses a string and returns an integer. The parseFloat() function parses a string and returns a floating-point number.
If the first character cannot be converted to a number, parseInt() and parseFloat() returns NaN.

//Example of parseInt()
parseInt("11")
// Output: 11
parseInt("11.29")
// Output: 11
parseInt("10 20 30")
// Output: 10
parseInt("Hello29")
// Output: NaN


//Example of parseFloat()
parseFloat("11")
// Output: 11
parseFloat("11.29")
// Output: 11.29
parseFloat("10 20 30")
// Output: 10
parseFloat("Hello29")
// Output: NaN
Enter fullscreen mode Exit fullscreen mode

Number Rounding

There are built-in functions for rounding:

  • Math.floor(): Moves down.
  • Math.ceil(): Moves Up.
  • Math.round(): Rounds to the nearest integer
Math.floor(2.1) // 2
Math.floor(-2.1) // -3

Math.ceil(2.1) // 3
Math.ceil(-2.1) //2

Math.round(4.1) // 4
Math.round(4.5) // 5
Math.round(3.6) // 4
Enter fullscreen mode Exit fullscreen mode

String

A string is a single or a series of characters. In javascript, strings are surrounded by quotes. There are three types of quotes.

  1. single Quotes
  2. Double Quotes
  3. Backticks
let string1 = 'Single quotes are ok too';
let string2 = "Double Quotes";
let string3 = `Backticks Quotes`;
Enter fullscreen mode Exit fullscreen mode

There are no difference between double and single quotes. They are simple quotes. But, Backticks strings are called template strings because variables and expressions can be embedded into them.

let name = "Rishu";
let greetings = `Hi!, ${name}`;
let a = 10, b=15;
let sum = `Sum = ${a+b}`;
Enter fullscreen mode Exit fullscreen mode

Case Change

There are two very helpful functions to change the case of a text.

  • toLowerCase()
  • toUpperCase();

Example of toLowerCase()

let text = "HI!, EARTH";
console.log(text.toLowerCase());
// Output: "hi!, earth"
Enter fullscreen mode Exit fullscreen mode

Example of toUpperCase()

let text = "Hello, everyone";
console.log(text.toUpperCase());
// Output: "HELLO, EVERYONE"
Enter fullscreen mode Exit fullscreen mode

Trimming a string

In javascript, we can use the trim() method to remove whitespace (spaces, tabs, etc) from both the start and end of a string. It is a method of the String object. It returns a string without start and end whitespaces.

let checkingString = "   Hello, How are you?     ";
console.log(checkingString.trim());
// Output: "Hello, How are you?"
Enter fullscreen mode Exit fullscreen mode

Check string

startsWith() and endsWith() are string method. With startsWith(), we can check Whether a string starts with the specified prefix and With endsWith(), we can check Whether a string ends with the specified suffix.

Example of startsWith()

let checkingString = 'Hello World!';

console.log(checkingString.startsWith('Hello')); // true
console.log(checkingString.startsWith('Heppo')); // flase
Enter fullscreen mode Exit fullscreen mode

Example of endsWith()

let checkingString = 'Hello World!';

console.log(checkingString.endsWith('World!')); // true
console.log(checkingString.endsWith('World')); // flase
Enter fullscreen mode Exit fullscreen mode

Discussion (0)