JS: functions arguments default values

In some languages you can set default values for function’s arguments:

function Foo(arg1 = 50, arg2 = 'default') {

How do you do it in JavaScript?


In JavaScript, anything that isn’t set is given the value undefined. This means that if you want to set default values for a function, your first line needs to be a check to see if those values aren’t defined:

function Foo(arg1, arg2) {
    if (typeof(arg1) === "undefined") { arg1 = 50; }
    if (typeof(arg2) === "undefined") { arg2 = "default"; }

You can take a bit of a short cut if you know roughly what those values will be:

function Foo(arg1, arg2) {
    arg1 = arg1 || 50;
    arg2 = arg2 || "default";

However, if those arguments are falsy, they’ll be replaced. This means that if arg1 is an empty string, null, undefined, false or 0 it’ll be changed to 50, so be careful if you chose to use it


Quotation marks not shown on display

In my string xml file I have a line like this:

<string name="infoHeader">random "text" here</string>

But it is only displayed as

random text here

on the phone’s display, that is, without the quotation marks.

I read about it via Google, and tried some HTML styling:

<string name="infoHeader">random &quot;text&quot; here</string>

And then used:

String hej = res.getString(R.string.infoHeader);     
Spanned marked_up = Html.fromHtml(hej);
((TextView)findViewById(, BufferType.SPANNABLE);

But it still doesn’t work. I tried some other HTML, like &lt;i>text&lt;/i>, and that works just fine. What’s wrong?


Try this:

<string name="infoHeader">random "text" here</string>

How to disable scrolling temporarily?

I’m using the scrollTo jQuery plugin and would like to know if it is somehow possible to temporarily disable scrolling on the window element through Javascript? The reason I’d like to disable scrolling is that when you scroll while scrollTo is animating, it gets really ugly 😉

Of course, I could do a $("body").css("overflow", "hidden"); and then put it back to auto when the animation stops, but it would be better if the scrollbar was still visible but inactive.


The scroll event cannot be canceled. But you can do it by canceling these interaction events:
Mouse & Touch scroll and Buttons associated with scrolling.

[Working demo]

// left: 37, up: 38, right: 39, down: 40,
// spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
var keys = {37: 1, 38: 1, 39: 1, 40: 1};

function preventDefault(e) {

function preventDefaultForScrollKeys(e) {
  if (keys[e.keyCode]) {
    return false;

// modern Chrome requires { passive: false } when adding event
var supportsPassive = false;
try {
  window.addEventListener("test", null, Object.defineProperty({}, 'passive', {
    get: function () { supportsPassive = true; } 
} catch(e) {}

var wheelOpt = supportsPassive ? { passive: false } : false;
var wheelEvent = 'onwheel' in document.createElement('div') ? 'wheel' : 'mousewheel';

// call this to Disable
function disableScroll() {
  window.addEventListener('DOMMouseScroll', preventDefault, false); // older FF
  window.addEventListener(wheelEvent, preventDefault, wheelOpt); // modern desktop
  window.addEventListener('touchmove', preventDefault, wheelOpt); // mobile
  window.addEventListener('keydown', preventDefaultForScrollKeys, false);

// call this to Enable
function enableScroll() {
  window.removeEventListener('DOMMouseScroll', preventDefault, false);
  window.removeEventListener(wheelEvent, preventDefault, wheelOpt); 
  window.removeEventListener('touchmove', preventDefault, wheelOpt);
  window.removeEventListener('keydown', preventDefaultForScrollKeys, false);

UPDATE: fixed Chrome desktop and modern mobile browsers with passive listeners


J2ME (Java) – Null Pointer Exception caught in Display Class

I’m currently working with MIDlets (I am using a Visual MIDlet) in Netbeans, and a NullPointerException is being thrown but I do not know why.

Note: The exception is not thrown when the program runs on the emulator, only when the OK Command button is pressed.

Here is the error I get

TRACE: <at java.lang.NullPointerException:   0>, Exception caught in Display class
java.lang.NullPointerException:   0
        at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=46
        at com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=74
        at com.sun.midp.chameleon.layers.SoftButtonLayer.soft1(), bci=37
        at com.sun.midp.chameleon.layers.SoftButtonLayer.keyInput(), bci=36
        at com.sun.midp.chameleon.CWindow.keyInput(), bci=38
        at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=17
        at com.sun.midp.lcdui.DisplayEventListener.process(), bci=277
        at, bci=179

I have stripped out all of the code unrelated to the exception, so that you can read it easier. Below is a simplified version if the code I have, which throws the above exception.

package stMidlet;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class StoryMidlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    private Command commandOk1;
    private Form form1;
    private TextField textField1;

    public StoryMidlet() {
        commandOk1 = new Command("Ok", Command.OK, 1);
        textField1 = new TextField("Enter value: ", null, 120, TextField.ANY);
        form1 = new Form(null, new Item[]{textField1});

/* There were some methods here pre-inserted by netbeans. */

/* I have not changed these, but I can post them if you need me too */

/* initialize() */

/* startMIDlet() */

/* resumeMidlet() */

/* switchDisplayable */

/* getDisplay() */

/* exitMidlet() */

/* startApp() */

/* pauseApp() */

/* destroyApp() */

    public void commandAction(Command c, Displayable d) {
        if (c == commandOk1)


I have been trying to solve this for at least an hour, with no prevail. The only thing I can think of worth mentioning is:

  • Netbeans showed a warning with the line Display.getDisplay(this)….. saying there was a leak in the constructor. I moved it into the initialize() method which sedated the warning, but the exception still occurs.

Any help will be greatly appreciated.

Thanks, Tom.


EDIT: I might need to redact my answer because I think what I said is not relevant, but I’ll leave it up on the off-chance that it could help!

It’s been a long time since I have worked in J2ME, but having looked at some old code I noticed I never did anything that useful in the constructor. I’m betting your call to Display.getDisplay(this) is causing a NullPtrException because something hasn’t been initialized yet. In fact, I’m pretty sure using the this pointer in a constructor is fairly certain to cause this type of error.

Try dealing with Display in the startApp() function, and if this code I’m referring to is correct, you should keep a boolean that marks if your MIDlet has been initialized yet or not.

You can look at some old code of mine here for reference:


Android button or TextView vertical align

i am trying to vertically align a button or a textview, but i can’t.
Neither android:layout_gravity="center_vertical|center_horizontal" nor android:gravity="center_vertical|center_horizontal" works. Not even android:gravity="center" does the trick.
They only aligns the object horizontally. How can i align it to the center or bottom of the view?


Depends on the parent layout. If you’re using a RelativeLayout, you can simply use layout_alignParentBottom="true", although layout_gravity="bottom" should work as well. Keep in mind the size of this parent layout as well. You can’t use alignParentBottom if the RelativeLayout is set to wrap_content for height. It should be set to fill_parent. You should post the rest of your XML if you’re still having issues.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..